htaccess to route all requests through index add add trailing slash

时间:2015-11-12 10:52:28

标签: apache .htaccess mod-rewrite url-rewriting url-redirection

This probably seems like quite a simple question to most of you, but i'm having a real hard time getting my site to route all requests through the index file whilst also enforcing trailing slashes. I've searched high and low and not found an appropriate solution. My .htaccess so far is as follows:

RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [QSA,L]

RewriteCond %{REQUEST_URI} /+[^\.]+$
RewriteRule ^(.+[^/])$ %{REQUEST_URI}/ [R=301,L]

This is as far as i have managed to get and works apart from it adds the index.php into the url, for example if a user goes to;

www.example.com/about-us

I would like it to route through index and become;

www.example.com/about-us/

However my above htaccess file produces;

www.example.com/index.php/about-us/

Seems so close yet so far, anything i do to the htaccess file to try and remove the index.php just stops it from working entirely. I am aware my solution is far from perfect, it still allows access to directories and files however its the best i've been able to come up with (would ideally like to deny directory listing!)

Any help is greatly appreciated!

1 个答案:

答案 0 :(得分:0)

在这些情况下,规则的排序非常重要。切换两个规则顺序,您应该得到所需的行为:

RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_URI} /+[^.]+$
RewriteRule ^(.+[^/])$ %{REQUEST_URI}/ [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [QSA,L]