简体   繁体   English

从URL删除php扩展名-使用htaccess只能删除一个URL

[英]Remove php extension from URL - except one and only one URL using htaccess

I'm using this code to remove the .php from the end of my URL's making them more SEF. 我正在使用此代码从URL末尾删除.php,使它们更像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. 这是通用的,并且对每个UR都适用,但是我想知道如何为一个和唯一一个URL创建一个例外 ,例如: http://www.domain.com/services : http://www.domain.com/serviceshttp://www.domain.com/services.php而不会影响其他所有。 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. 添加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) 删除索引索引中的扩展名.php已被裁剪)

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)/index/?([^\ \?]*) [NC]
RewriteRule ^ %1/%2 [R=301,L]

You should : 你应该 :

  • add a RewriteCond , to disabled services.php => services RewriteCond添加到禁用的services.php => services
  • add another rule, to enforce services => services.php , as the first rule doesn't prevent user to manually use services url 添加另一条规则,以强制执行services => services.php ,因为第一条规则不会阻止用户手动使用services url

Full .htaccess 完整的.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]

I would believe just a small change in your .php removal rule should do the job: 我相信只需对您的.php删除规则进行少量更改即可完成此工作:

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

Can you try this, 你可以试试这个吗

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

You can add one rule for each php file: 您可以为每个php文件添加一个规则:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM