1

I was using .htaccess code to remove .php extension for all my web pages. Here's the code I use:

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

It doesn't seem to work. I think I'm missing something. When I type www.mysite.com/about/ to get www.mysite.com/about.php it returns error 404 (page not found). Can someone please shed some light.

Thanks, Paul G.

0

2 Answers 2

4
Options +FollowSymLinks -MultiViews

RewriteEngine On
RewriteBase /

# If folder does not exist
RewriteCond %{REQUEST_FILENAME} !-d
# and file exist
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f
# uncomment the below rule if you want the "/" to be required 
# otherwise leave as is
# RewriteRule ^([^/]+)/$ $1.php [L]
# internally show the content of filename.php
RewriteRule ^([^/]+)/?$ $1.php [L]

The above rule will:

  1. will not redirect if a folder exist
  2. will not redirect if the file does not exist
  3. will redirect what comes before the / if one is present as the file name

So it will work for all these examples:

http://domain.com/about/
http://domain.com/about
http://domain.com/contact/
http://domain.com/contact

If you want you can remove the ?, like the commented rule, to make it accept only URL's that end with a /.

http://domain.com/about/
http://domain.com/contact/

Now these are important step for the above to work:

  1. It must go into the .htaccess on your root folder for example /home/youraccount/public_html/.htaccess
  2. The Options before the rewrite rule are very important specially -MultiViews
  3. The file must exist on the same place the .htaccess is for example in your case the about.php file
  4. The PHP must be working obviously.
Sign up to request clarification or add additional context in comments.

8 Comments

It works without the trailing slash "/", once I add slash, it loads the page without styling, and when navigate to another page it returns 404 error, and the url tunrs to www.mysite.com/about/services.
@netizen0911 that's because youre using relative paths for css, images and javascript, instead of using css/my.css you need to use the full URL like this http://domain.com/css/my.css on your HTML. When using relative URL like css/my.css it will assume that the file is on the same folder in your case it would assume http://domain.com/about/css/my.css and that's why it fails.
Yup, I figured that one out, but how about getting 404 error when navigating from the address where there's a trailing slash "/" at the end? (i.e. when I have www.mysite.com/about/ up and click on contact.php link, it gives www.mysite.com/about/contact/ instead of www.mysite.com/contact/)
@netizen0911 same reason you're using relative path for your URL's you need to use full path. <a href="/contact/">contact</a> will give you one result while <a href="contact/">contact</a> will give a different one from <a href="http://domain.com/contact/">contact</a>, same goes for images, CSS, JavaScript and any other resources.
Its good to have 1 browser you never use around for this things, if browser A is cached you check with browser B to make sure, as for multi-level folders, the rule would be different, RewriteRule ^(.+?)/?$ $1.php [L] just replace the above RewriteRule with this one and keep the 2 RewriteCond.
|
0

It seems like the slash at the end of your rule might be there, or it might not. Adding a ? makes it optional, so that mysite.com/about and mysite.com/about/ will both match.

RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-\s]+)/?$ /$1.php

It's hard to say if this is what's causing your problem, or if something else is, though. Does mysite.com/about.php also give you an error?

Comments

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.