0

I'm working in a directory called testsite and I want all .php extensions to be replaced by a trailing slash. I'm halfway there, in that (for example) entering http://mydomain.com/testsite/about means that http://mydomain.com/testsite/about.php is loaded.

However, I now want the URL to be displayed as ./about/ so that only one version shows up in search engine rankings.

Here's my .htaccess:

RewriteEngine On
RewriteBase /testsite/
RewriteRule ^()$ index.php [NC,L]
RewriteCond %{REQUEST_URI} !(^/?.*\..*$) [NC]
RewriteRule (.*)$ $1.php [NC]

Also, is it possible to preserve (and hide from display any parameters that I pass)?

All help is much appreciated!

2 Answers 2

1

Try these rules:

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule .*[^/]$ %{REQUEST_URI}/ [L,R=301]
RewriteRule ^$ index.php [L]
RewriteRule (.*)/$ $1.php [NC]
Sign up to request clarification or add additional context in comments.

4 Comments

Hi Gumbo. Thanks for this. I've tried this but it seems to add %25 to the end of a url such as ./about ( giving ./about%25 ) and it gets lost in a loop of ./about%25%25%25... when I put ./about/ in the address bar. I think it's almost there but not quite.
Hi Gumbo. Thanks for helping. It's getting closer. If I go to ./about it correctly puts ./about/ in as the url but the page shows without loading the CSS?!? If I then click on a link, say ./contact-us/ it goes nuts with ./about/contact-us.php.php.php.php.php !!! It must be nearly right so I'll have a look later today when I have more time. Thanks again.
@Outerbridge Mike: The first issue is probably due to relative URL references that are resolved from the current URL; so resolving ./contact-us/ on the base path /about/ results in /about/contact-us/. Just use absolute paths instead. And the second issue: Well, since a redirect takes only place if appending .php to the current file path references an existing file (although URL and file system path do not need to be the same), you might change that condition and use some other criterion or remove that entire condition.
Thanks again Gumbo. I replaced all the relative paths with absolutes and your solution works a treat. Many thanks for taking the time to help me.
0

Try replacing the last rule with:

RewriteRule (.*)$ $1.php [NC] [E=ORIGINAL_URL:$1]

2 Comments

Hi David. Unfortunately I'm still getting the same results as before. What does the E= part do? Thanks Mike
E or env is an environment variable, check httpd.apache.org/docs/2.2/env.html for more details. In example above, original URL is preserved even though a rewrite is made.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.