aserwin
aserwin

Reputation: 1050

Looking for advice on mod_rewrite and htaccess

I am not experienced with htaccess... But I am authoring a site on which I am using a simple redirect. I basically just copied the first htaccess that I found and it works fine, but I am curious if I am using it properly and/or if it is responsible for some problems I am having with the site.

This is my file..

RewriteEngine On

Exclude some directories from URI rewriting
RewriteRule ^(dir1|dir2|dir3) - [L]

RewriteRule ^\.htaccess$ - [F]

RewriteCond %{REQUEST_URI} =""
RewriteRule ^.*$ /src/index.php [NC,L]

RewriteCond %{REQUEST_URI} !^/src/.*$
RewriteRule ^(.*)$ /src/$1

RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^.*$ - [NC,L]

RewriteRule ^src/.*$ /src/index.php [NC,L]

The index.php (as you can tell) lives in /src), as does most pf the site. One problem I am having is that accessing some of the includes results in pulling back the index.php!

Any advice, or directions to some tuts or articles on htaccess would be GREATLY appreciated.

Thanks

Upvotes: 0

Views: 90

Answers (2)

Jon Lin
Jon Lin

Reputation: 143876

Try combining a few of the rules:

RewriteEngine On

# Exclude some directories from URI rewriting
RewriteRule ^(dir1|dir2|dir3) - [L]

# block access to the htaccess file
RewriteRule ^\.htaccess$ - [F,L]

# route "/" requests to index.php
RewriteRule ^$ /src/index.php [L]

# route requests that exists in /src/ to /src/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/src%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}/src%{REQUEST_URI} -d
RewriteRule ^(.*)$ /src/$1 [L]

# route everything else to /src/index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /src/index.php [L]

Upvotes: 0

anubhava
anubhava

Reputation: 785058

Have to say that the lot of the code in here looks redundant. Replace your code with this:

RewriteEngine On

RewriteRule ^\.htaccess$ - [F]

RewriteCond %{REQUEST_URI} ^/(dir1|dir2|dir3) [OR]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]

RewriteCond %{REQUEST_URI} !^/src/.*$
RewriteRule ^(.*)$ /src/$1 [L]

RewriteRule ^src/.*$ /src/index.php [NC,L]

Also when you say that ccessing some of the includes results in pulling back the index.php! I hope you are just including them like:

include('path/to/somefile.php');

That won't go through .htaccess since this inclusion is transparent to Apache.

Upvotes: 1

Related Questions