Multiple rewrite conditions and folder exception in .htaccess file - regex

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]

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.

htaccess does not work properly

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]

remove folder from url using .htaccess

I know there are plenty of other questions similar to this one, i tried followinf some of those, but with no luck.
I have a website called test.com and i need, when someone seraches for test.com to show the content in test.com/home, without having home in the uerl.
My htaccess file at the moment looks like this:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule /(.*) home/$1 [L]
RewriteRule /home/(.*) /$1 [L,R=302]
RewriteOptions inherit
RewriteCond %{HTTP_HOST} ^test\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.test\.com$
but when i type test.com it shows a list of folders, not the content inside home.
What am i doing wrong?
thanks
You can simplify your rules to this in root .htaccess
RewriteEngine on
RewriteBase /
RewriteRule ^$ home/ [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^((?!home/).+)$ home/$1 [L,NC]
Make sure to clear your browser cache before testing this.

Prettifying a URL is failing

So I have a URL
http://domain.com/directory/
There is an index.php inside of directory which allows me to access that url as it is
But say I want to access http://domain.com/directory/?id=123 but from this http://domain.com/directory/123
I tried the below both in the root and also in the directory folder but still no luck
RewriteEngine On
RewriteRule ^directory/([^/]*)$ /directory/?id=$1 [L]
Place this rule in /directory/.htaccess:
RewriteEngine On
RewriteBase /directory/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ ?id=$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]