0

I'm using this code to remove the .php from the end of my URL's making them more SEF.

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/(.+)\.php[^\s]* [NC]
RewriteRule ^ /%1 [R=301,NE,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [QSA,NC,L]

This is generic and works perfectly for every UR but I would like to know how to create an exception for one and only one URL, ie: http://www.domain.com/services to http://www.domain.com/services.php without influencing all the others. Is this possible?

I've tried a simples Redirect but entered a loop.

! EDITED !

I failed to mention I have also these rules:

Add www.

RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

Remove index (the extension .php from index has been trimmed already)

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)/index/?([^\ \?]*) [NC]
RewriteRule ^ %1/%2 [R=301,L]
2
  • So you don't want /services to be rewritten as /services.php ? Commented Nov 14, 2013 at 10:42
  • I'm sorry, I'll try to explain better.I have my website working as planned everything .php rewritten with out it. I want one and only one URL, in this case services, to have the php extension Commented Nov 14, 2013 at 21:03

4 Answers 4

2

You should :

  • add a RewriteCond, to disabled services.php => services
  • add another rule, to enforce services => services.php, as the first rule doesn't prevent user to manually use services url

Full .htaccess

# domain.tld > www.domain.tld (visible)
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

# services > services.php (visible)
RewriteRule ^services/?$ services.php [R=301,QSA,L]

# filename > filename.php (transparent)
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [QSA,NC,L]

# filename.php > filename (visible, exception for services.php)
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/(.+)\.php[^\s]* [NC]
RewriteCond %{REQUEST_FILENAME} !services\.php
RewriteRule ^ /%1 [R=301,NE,L]

# index > /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)/index/?([^\ \?]*) [NC]
RewriteRule ^ %1/%2 [R=301,L]
Sign up to request clarification or add additional context in comments.

4 Comments

I just retried it on a blank website and it seems to work. Have you got any other .htaccess file/rule ?
Hi zessx thanks for the input. I do have more rules maybe influencing: #TO TRANSFORM NON WWW TO WWW # RewriteCond %{HTTP_HOST} !^www\. RewriteRule ^(.*)$ %{HTTP_HOST}/$1 [R=301,L] # AND TO REMOVE THE INDEX # RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)/index/?([^\ \?]*) [NC] RewriteRule ^ %1/%2 [R=301,L]
Ok I find my mistake. For the second block, the 2 RewriteCond are only applied to the first next RewriteRule. See my edit for a working .htaccess
It did work the whole package, thank you very much for your time!
1

I would believe just a small change in your .php removal rule should do the job:

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/(.+?)\.php[\s?] [NC]
RewriteRule !^services\.php$ /%1 [R=301,NE,NC,L]

1 Comment

Actually yes, this simples line just did the trick! Thank you very much
0

Can you try this,

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

2 Comments

Where should I mention the one php file in particular that I want the .php to appear on the URL?
You need to provide the link like, <a href='http://www.domain.com/services'>Service</a>, it recognize the service.php automatically.
0

You can add one rule for each php file:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^services  /services.php [L]

3 Comments

Nothing happens on this case
Sorry, It was "services", not "statistics".
Thanks Joan I've noticed and I did tried it with the services not the statistics.

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.