0x_Anakin
0x_Anakin

Reputation: 3269

htaccess - trailing slash url

Hello I have the following problem: I have some pages (php includes) which I load them like this

http://www.domain.com/news/

they work perfect. But if I remove the trailing slash

http://www.domain.com/news

this happens -> http://www.domain.com/news/?page=news&request=

Here are my htaccess rules:

RewriteEngine on

<IfModule mod_gzip.c>
    mod_gzip_on       Yes
    mod_gzip_dechunk  Yes
    mod_gzip_item_include file      \.(html?|txt|css|js|php|pl|jpg|png|gif)$
    mod_gzip_item_include handler   ^cgi-script$
    mod_gzip_item_include mime      ^text/.*
    mod_gzip_item_include mime      ^application/x-javascript.*
    mod_gzip_item_exclude mime      ^image/.*
    mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
</IfModule>




Options -Indexes
RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)(.*)$ /index.php?page=$1&request=$2

PS. Could it by because I have a news folder in the root document as well ?

Upvotes: 0

Views: 807

Answers (2)

Jon Lin
Jon Lin

Reputation: 143936

This is a mod_dir issue. It's redirecting the browser when it thinks a URL maps to a directory (even if it's later determined not to) to ensure that a trailing slash is appended to the end. See this recent explanation that I posted in another question.

Like in the other answer, you can either turn off DirectorySlash or ensure that all of the affected URL's get redirected with a trailing slash via mod_rewrite (so that the rewrite and the redirect happens within the same module):

  1. Turn off mod_dir by including a DirectorySlash Off. This makes it so mod_dir won't redirect the browser, but note that there are other consequences to turning this off. You can add that directive in your htaccess file.

  2. Handle the trailing slash in mod_rewrite:

    RewriteEngine On
    Options -MultiViews
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_URI} !/$
    RewriteRule ^(.*)$ /$1/ [L,R=301]
    

Upvotes: 0

BenM
BenM

Reputation: 53248

Yes, it's because you have a news/ folder in the root. Your Rewrite Condition is looking for anything that isn't a file (i.e. !-f) or a directory (!-d). Try renaming your news/ directory in the root.

If you must, you can force domain.com/news to rewrite to domain.com/news/ by doing the following:

RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://domain.com/$1/ [L,R=301]

Upvotes: 1

Related Questions