1

I need to remove the .php from all the urls on my website, and if someone types in a full url it needs to redirect to the php-extension-free version. Eg:

website.com/about.php -> website.com/about

I need to do this in as SEO friendly a manner as possible, so I'm guessing that would be a 301 redirect so Google and others know that the page has a new location at the new php-extension-free URL. Any dup content would also have to be avoided, so it's important that the page isn't accessible at both the old and new urls, which is what would happen if all I did was a simple:

RewriteRule about about.php [L]

I've seen a number of .htaccess approaches for this, but they all seem to add a trailing slash to the URL. It's important that this doesn't happen too.

Also, I have an explicit HTTPS redirect happening for a couple pages, and I need to make sure I don't create a redirect loop. Here's the code that's currently redirecting those pages to HTTPS.

RewriteCond %{HTTPS}  off
RewriteRule  ^(quote|quote_2).php$  https://website.com/$1.php  [R=301,L,QSA]
RewriteCond %{HTTPS}  on
RewriteCond %{REQUEST_URI}   !^/(quote|quote_2).php
RewriteCond %{REQUEST_URI}   !^/(.*)\.(css|png|js|jpe?g|gif|bmp|woff|svg|map)$
RewriteRule  ^(.*)$  http://website.com/$1  [R=301,L,QSA]
5
  • 1
    I don't see a question here. If you need it, hire a programmer Commented Feb 26, 2014 at 22:30
  • The question is a clear one, and similar to most every other question on this site. I need a specific desired behavior, and have posted the code I currently use that has failed to produce said behavior. Commented Feb 26, 2014 at 22:48
  • Usually questions end in a ?. Commented Feb 26, 2014 at 22:48
  • It is all about the tone and your prior research. You've done some research, that sets you apart from other questions, correct, but the tone is quite demanding. It doesn't sound like "I've tried this and that, why doesn't it work?/How does it work properly?" etc. but more like "I need this. I read somewhere that it can be done, do it for me" Commented Feb 26, 2014 at 22:56
  • 1
    I'm sorry if I came across that way. I've actually tried to solve this particular problem via an .htaccess redirect 3-4 times over the past six months, and have been unsuccessful so far. I greatly appreciate any help. Commented Feb 26, 2014 at 23:00

2 Answers 2

1

You can use this code in your root .htaccess:

RewriteEngine On

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

RewriteCond %{HTTPS} off
RewriteRule ^(quote|quote_2)/?$ https://%{HTTP_HOST}/$1 [R=301,L]

RewriteCond %{HTTPS}  on
RewriteCond %{REQUEST_URI} !^/(quote|quote_2)
RewriteCond %{REQUEST_URI} !\.(css|png|js|jpe?g|gif|bmp|woff|svg|map)$
RewriteRule  ^(.*)$  http://%{HTTP_HOST}/$1  [R=301,L]

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

3 Comments

That's great, thanks! Another quick question: I'm explicitly redirecting a couple specific pages to https, and that's causing a redirect loop when combined with the above. Here's my code: RewriteCond %{HTTPS} off RewriteRule ^(quote|quote_2).php$ https://lawnlove.com/$1.php [R=301,L,QSA] RewriteCond %{HTTPS} on RewriteCond %{REQUEST_URI} !^/(quote|quote_2).php RewriteCond %{REQUEST_URI} !^/(.*)\.(css|png|js|jpe?g|gif|bmp|woff|svg|map)$ RewriteRule ^(.*)$ http://lawnlove.com/$1 [R=301,L,QSA]
Difficult to read code from comment, can you post it in your question by editing it.
Let me know if there is any issue.
0

The below answer can be found here

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

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.