TimSim
TimSim

Reputation: 4036

Apache - Rewrite rule changes URL

In my .htaccess file, I have

RewriteEngine On
RewriteCond %{REQUEST_URI} !\.(swf|gif|jpe?g|png|css|txt|js|json|xml|ico)$
RewriteRule ^(.*)/?$ index.php?page=$1 [QSA,L]

If I have a directory called myfiles and in the browser address bar I go to http://localhost/mysite/myfiles/ everything is fine, but if I go to http://localhost/mysite/myfiles (without the trailing slash), the URL in the address bar changes to http://localhost/mysite/myfiles/?page=myfiles

How do I change the .htaccess so that it doesn't change the URL and expose the variable? And why does it do that in the first place?

Upvotes: 0

Views: 122

Answers (1)

Jon Lin
Jon Lin

Reputation: 143896

This is because mod_dir is redirecting requests for directories that don't end in a slash to ones that do, and there's a very good reason why it must do this.

What you'll need to do is either turn off mod_dir and/or redirect those slashes yourself:

DirectorySlash off

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+[^/])$ /$1/ [L,R=301]

RewriteCond %{REQUEST_URI} !\.(swf|gif|jpe?g|png|css|txt|js|json|xml|ico)$
RewriteRule ^(.*)/?$ index.php?page=$1 [QSA,L]

Upvotes: 1

Related Questions