3

I've been searching all day for a solution to replace .php in the URL with a / using .htaccess. Most of the solutions didn't work for me at all (didn't rewrite the URL, even just to remove .php) until I found this beautiful solution on SO.

https://stackoverflow.com/a/11084526/1724376

Now my issue is that it only removes the .php but does not replace it with a "/". I've tried many things with no luck but I don't know much about htaccess and rewrite conditions, etc. I'm really hoping someone here can help me.

Just so I don't get down-voted for not having tried anything, here's one that I tried but it didn't rewrite the URL at all.

RewriteCond /%{REQUEST_FILENAME}.php -f
RewriteRule ^([a-zA-Z0-9_-\s]+)/$ /$1.php

Help will be truly appreciated.

EDIT: To clarify, I want www.mysite.com/contact.php to show up as www.mysite.com/contact/

9
  • A lot of people miss the fact that there are two basic types of rule you can set with mod_rewrite; often, you want both. One type takes a pretty URL typed into a browser, and calculates the ugly version that should be served instead, but is completely invisible to the browser; the other type takes an old ugly URL, and tells the browser to go to a pretty URL instead. The second type is only useful if you have the first type working. The rule you post here is the first type - if you go to a URL ending in / it will try to serve a PHP file, but it will not redirect the browser in any way. Commented Jun 21, 2014 at 23:51
  • Thanks, yes that makes sense. I'm guessing the ones with [R=301] do a 301 redirect. This led me to try RewriteRule (.*)\.php$ $1/ [R=301] but of course that didn't work since example.com/test/ doesn't exist when I try to access example.com/test.php. Still looking for a solution, but at least this sparked some more ideas :) Commented Jun 21, 2014 at 23:58
  • Yep, [R=301] means a 301 HTTP redirect; without an [R] flag the rewrite is internal, and you'll often see [L], meaning "last", to stop both rules firing at once (they're processed in order, so the other option is to be careful how you list them). You say "of course ... example.com/test/ doesn't exist" - that's the point of the rule you've pasted here, to make that URL exist. My advice: get the "pretty" URLs working if you link to them directly first, then worry about redirecting people's browsers if they access the "legacy"/"ugly" URLs instead. Commented Jun 22, 2014 at 0:04
  • Making the URLs exist is exactly what I'm trying to avoid though. That's a solid few days of work and then retesting the entire site to make sure everything works. I was hoping htaccess would be the solution here. All I need to do is have example.com/test.php be accessible as example.com/test/ for two reasons - 1) Pretty URLs, and 2) Security by obfuscation (which I know isn't good but it can't hurt). Commented Jun 22, 2014 at 0:09
  • I'm confused: "Making the URLs exist" and "have example.com/test.php be accessible as example.com/test/" are the same thing! If you can access example.com/test/, it exists (as far as the browser is concerned). Then you can do whatever you like to tell the browser to load that page (e.g. put in place 301 redirects from "old" URLs). Commented Jun 22, 2014 at 0:16

1 Answer 1

1

Have your rule like this:

RewriteEngine On

RewriteCond %{THE_REQUEST} \s/+(?:index)?(.*?)\.php[\s?] [NC]
RewriteRule ^ /%1/ [R=302,L,NE]

RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ /$1.php [L]
Sign up to request clarification or add additional context in comments.

8 Comments

Hi Anubhava. Thanks for the reply. I tried it and it still only removes the .php from the URL but doesn't append the slash. I'm wondering if there's something different about my server configuration.
It does place the slash in the end, you need to test this in a new browser to avoid 301 caching problem. Also are you sure you don't have any other rule removing trailing slash?
My entire htaccess is just what you have up there so nothing else is removing the training slash. I tried it again, cleared cache and temporary internet files and tried IE instead of Chrome, same result :( you can see it in action at depts (d0t) org
When I sent a request for http://depts.org/news.php it became http://depts.org/news/ with trailing slash in the end.
Thanks, yes that's a good point. This is a development environment so I was avoiding it but will be updating that next using PHP variables to make it easy to update.
|

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.