htaccess does not work properly - regex

On my WebServer I have a htaccess file which looks like this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^?]*)$ /index.php?path=$1 [NC,L,QSA]
So all calls to my WebServer are redirected to the index.php file in my root directory with a path parameter which holds the requested file. Now look at these two calls:
domain.com/test/blub.php works -> path = test/blub.php
domain.com/test/ does not work -> path should be test
How can I achieve the the second call also works?

Remove this condition from your rule:
RewriteCond %{REQUEST_FILENAME} !-d
As this is skipping all real directories from this rule.
Use your rule as:
DirectorySlash Off
RewriteEngine on
RewriteCond %{DOCUMENT_ROOT}/$1 -d
RewriteRule ^(.*?[^/])$ %{REQUEST_URI}/ [L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)/?$ /index.php?path=$1 [NC,L,QSA]

Related

Mod re-write forward empty directory and convert paths to parameters

I currently use this to move from example.com/nodirectory where there is no directory to example.com/user?user=nodirectory where this is a user directory:
RewriteCond %{LA-U:REQUEST_FILENAME} !-f
RewriteCond %{LA-U:REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ /user/index.html?user=$1 [QSA,L,R=301]
I am trying to extend this so that a URL example.com/nodirectory/value1 would end up as example.com/user?user=nodirectory&param1=value1
RewriteRule ^([^/]+)/([^/]+)/?$ /user/index.html?user=$1&param1=$2[QSA,L,R=301]
The above just cause too many redirects.
RewriteCond %{LA-U:REQUEST_FILENAME} !-f
Should be
RewriteCond %{REQUEST_FILENAME} !-f
Because the rule is in a htaccess file, the first directive would work if it were in the httpd.conf file.

Unexpected behavior with custom .htaccess rewrite rules

So I have been working on some rewrite rules which aren't working as expected. These are some sample requests I am trying to rewrite:
/ -> /index.php?page=Home
/Home -> /index.php?page=Home
/Teaching/Foo -> /index.php?page=Teaching&id=Foo
/Teaching/Bar -> /index.php?page=Teaching&id=Bar
/Download/ue8 -> /index.php?action=Download&id=ue8
/Download/24a -> /index.php?action=Download&id=24a
(default) -> /index.php?page=Home
** OR ALTERNATIVELY **
(default) -> /index.php?page=FileNotFound
(and maybe rewrite the visible URL to /FileNotFound)
I mainly want to hide as much of the urls as possible and prevent both directory listing and direct access to my files located in specific folders and only allow access to downloadable files via /Download/FileId while having my usual pages for different lectures accesible via /Teaching/SomeLecture.
So far I have been using this snippet for the /Home and /Teaching stuff:
RewriteEngine On
RewriteBase /
# Redirect Trailing Slashes
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^Teaching/([A-Za-z0-9]*)$ index.php?page=Teaching&id=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^Home$ index.php?page=Home [L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)/index\.php [NC]
RewriteRule ^ %1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [L,R=301]
I am not completely sure about all of these directives and I noticed, that there are currently some flaws to it.
Trying to access a non-existant file, e.g. /Files/Bad/Path.pdf, forwards the user to /?page=Home which should be redirected either to /, /Home or display the contents of /index.php?page=FileNotFound without changing the URL at all or redirecting to /FileNotFound depending on the rule of (default). I am not really sure which solution might be the most suitable in this scenario.
Trying to access some folders which do exist results in an infinite redirection loop while folders which do not exist apparently redirect to /. In both cases it feels right to redirect to /FileNotFound I suppose?
Could you please work out a set of rules that might suit my needs in this case?
You have many redundant directives in your .htaccess. Replace all of your .htaccess with this:
# Turn off mod_spelling
<IfModule mod_speling.c>
CheckSpelling off
CheckCaseOnly off
</IfModule>
Options -MultiViews
RewriteEngine On
RewriteBase /
# block direct access to file and directories in these directories
RewriteCond %{REQUEST_URI} !\.(?:jpe?g|gif|bmp|png|tiff|css|js)$ [NC]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(Templates|Files) - [NC,F]
# remove index.php
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^(.*?)index\.php$ /$1 [L,R=301,NC,NE]
# Redirect Trailing Slashes
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(Download|Teaching)/([\w-]+)/?$ index.php?page=$1&id=$2 [L,QSA,NC]
RewriteRule ^(Home)?/?$ index.php?page=Home [L,NC,QSA]
Make sure to clear your browser cache before testing this.

Multiple rewrite conditions and folder exception in .htaccess file

I'm working on my website and facing some problems with .htaccess files.
I got a main directory(in my case public_html) with sub directory called admin.
What I'm trying to manage is that if end-user enters URL like example.com/admin, he will be redirected to that admin directory which contains login form, custom index.php file and so on.
If other url has been entered, it needs to be checked by some conditions.
Here are my .htaccess files:
For main directory:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^([^/]+)/([^/]+)/([^/]+)$ index.php?menu=$1&op=$2&id=$3 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^([^/]+)/([^/]+)$ index.php?menu=$1&op=$2 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^([^/]+)$ index.php?menu=$1 [QSA,L]
For admin directory:
RewriteEngine off
Right now main directories .htaccess file works properly after entering urls like:
example.com/news/article/4
example.com/news/list
example.com/home
but if I enter example.com/admin, its associated with third rewrite rule and thinks that 'admin' is a menu link.
I have tried different exceptions, but nothing worked for me, admin is still associated with menu not directory. Maybe my main .htaccess file has some mistakes? Could some one suggest me the right solution for this issue?
You don't need any .htaccess in admin directory. Just have these rules in root .htaccess:
DirectorySlash On
DirectoryIndex index.php
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ index.php?menu=$1&op=$2&id=$3 [QSA,L]
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?menu=$1&op=$2 [QSA,L]
RewriteRule ^([^/]+)/?$ index.php?menu=$1 [QSA,L]

htaccess ignore specific directory in URI - treat same as root

been looking for several hours now without success.
I'd like all urls containing /en/ to ignore this part but continue with all other rules in the htaccess (like php to no extension rule).
EXAMPLE
www.domain.com/en/gallery -> should point to gallery.php in my server root
www.domain.com/gallery -> should ALSO point to gallery.php in my server root
(will have to take care of duplicate content here somehow later)
www.domain.com/en/contact -> should point to contact.php in my root
Thank you
U P D A T E
Like mentioned in my question I'd like all other rules to still be running.
Only this rule does not work with below solution as it should work with
www.domain.com/en/category1 AND www.domain.com/category1 so with or without EN.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !(\.png|\.jpg|\.gif|\.jpeg|\.bmp|\.css|\.js|\.ico)$
RewriteRule ^([^/]+)/? /categories.php?rw=1&url=$1 [L,QSA]
Put this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(?:en/)?([^/]+)/?$ $1.php [L]
UPDATE:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !\.(png|jpe?g|gif|bmp|css|js|ico)$
RewriteRule ^(?:en/)?([^/]+)/?$ categories.php?rw=1&url=$1 [L,QSA]

RewriteCond to skip rule if file or directory exists

I am trying to adjust my htacess file to skip the rewrite rule if the file or directory exists, but when the file exists, it changes the URL to something different. I don't know why this is happening. Here is my htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f [OR]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^project/([^/\.]+)/?$ index.php?view=project&project=$1 [L]
RewriteRule ^for/content/ag index.php?view=opening&section=ag [L]
RewriteRule ^for/content/bus index.php?view=opening&section=bus [L]
RewriteRule ^for/content/fcs index.php?view=opening&section=fcs [L]
RewriteRule ^for/content/market index.php?view=opening&section=market [L]
RewriteRule ^for/content/trade index.php?view=opening&section=trade [L]
RewriteRule ^for/content/teched index.php?view=opening&section=teched [L]
RewriteRule ^for/content/([^/\.]+)/?$ index.php?view=content_area&section=$1 [L]
The directory /project/nti exists, with an index.php file. When the address mywebsite.com/folder/project/nti is entered, it changes to mywebsite.com/folder/project/nti/?view=project&project=nti. I am applying this on a test server where the sample site is in a subfolder, but when implemented, it will be in the root web server folder.
Thanks for any help!
Remove extra [OR] and have your code like this:
# If the request is not for a valid directory
RewriteCond %{REQUEST_FILENAME} !-d
# If the request is not for a valid file
RewriteCond %{REQUEST_FILENAME} !-f
# If the request is not for a valid link
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^project/([^/.]+)/?$ index.php?view=project&project=$1 [L,NC,QSA]