Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 245
Nathan Wittmaier Avatar asked Apr 05 '12 14:04

Nathan Wittmaier


People also ask

What is RewriteCond %{ Request_filename?

RewriteCond %{REQUEST_FILENAME} !-f. RewriteCond %{REQUEST_FILENAME} !-d. … means that if the file with the specified name in the browser doesn't exist, or the directory in the browser doesn't exist then procede to the rewrite rule below.

What is IfModule Mod_rewrite C?

The <IfModule mod_rewrite. c>... </IfModule> block ensures that everything contained within that block is taken only into account if the mod_rewrite module is loaded. Otherwise you will either face a server error or all requests for URL rewriting will be ignored.

What is RewriteCond in htaccess?

htaccess rewrite rules can be used to direct requests for one subdirectory to a different location, such as an alternative subdirectory or even the domain root. In this example, requests to http://mydomain.com/folder1/ will be automatically redirected to http://mydomain.com/folder2/.

What is NC in RewriteCond?

NC = nocase means regardless of the case of the incoming referrer traffic match with the given pattern. See Apache [NC|nocase] flag.


1 Answers

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]
like image 96
anubhava Avatar answered Sep 25 '22 10:09

anubhava