Erty Seidohl
Erty Seidohl

Reputation: 4559

Help with simple RewriteRule problem

I am creating a very small site, 5 pages, in php on apache. I've created the five php files in the top directory, index.php, random.php, etc. I wanted to hide the .php extension, so I put the following into my .htaccess:

RewriteRule ^random/?$ random.php

If I visit www.example.com/random I get the page I needed, but if I visit www.example.com/random/ (slash at the end), the css and links are all one directory down, i.e. the server thinks I am in /random/index.php, not at /random.php.

I'm a total noob at RewriteRule, so thanks in advance!

Upvotes: 1

Views: 102

Answers (1)

Laurence Gonsalves
Laurence Gonsalves

Reputation: 143154

You should have a separate rule for the trailing slash that actually does an HTTP redirect. eg:

RewriteRule ^random/$ /random [R=301,L]
RewriteRule ^random$ random.php

Relative URLs are handled by the browser, so with that trailing slash the browser will get confused.

Most HTTP servers actually do the inverse of this sort of redirect for directory names. That is, if you go to http://example.com/foo/bar and bar is a directory, you'll be redirected to /foo/bar/.

Upvotes: 2

Related Questions