4

I'm using mod_rewrite to rewrite /products to /products.php. I've got this code in /.htaccess

Options FollowSymLinks
RewriteEngine on

RewriteRule ^([a-zA-z]+)$ /$1.php [PT,L]

Unfortunately there is also a folder /products/ on my server.

My problem is, when I try to access http://mydomain.com/products my request is redirected to http://mydomain.com/products/ and showing me an error because I don't have an index for that directory.

Who is redirecting me? Apache, my UserAgent? How do I prevent that this happens without changing the folder name or the rewrite rule?

1
  • 1
    Using mod_rewrite how? Show us the mod_rewrite rules please - in some cases, existing files and folders take precedence over mod_rewrite. May 9, 2011 at 15:33

3 Answers 3

5

You need to look up the "DirectorySlash Directive".

The DirectorySlash directive determines whether mod_dir should fixup URLs pointing to a directory or not.

http://httpd.apache.org/docs/2.2/mod/mod_dir.html

You could also try adding an optional slash to you rewrite rule:

RewriteRule ^([a-zA-z]+)/?$ /$1.php [PT,L]

Trailing slashes problem

3
  • I added "DirectorySlash Off" to the .htaccess file, but it's still redirecting. May 9, 2011 at 15:45
  • @esel try the other solution. maybe its easier for you.
    – zaf
    May 9, 2011 at 15:58
  • Yes that's exactly the same idea I got from Marms post. Thanks. May 9, 2011 at 16:00
2

You have probably enabled MultiViews on your Apache.

0
0

Every browser is adding the trailing slash after your request if it doesn't by ".something" because it thinks it is a folder. To avoid this, your rewrite rule should look like this:

RewriteRule /products(.*)$ /index.php?page=products

OR

RewriteRule /products /index.php?page=products

That way, it will rewrite every request with "/products" in it, with or without the trailing slash.

The only thing is your folder /products/ will not be accessible by an http request. If you want so, you must change the name of the folder or the page name.

1
  • Thanks. I can change my rule to RewriteRule ^([a-zA-z]+)\/?$ /$1.php [PT,L] so only /products and /products/ is rewritten, not other requests to the folder. May 9, 2011 at 15:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.