简体   繁体   English

htaccess重写(删除php扩展名)

[英]htaccess Rewrite (remove php extension)

today I have a problem with my .htaccess file. 今天,我的.htaccess文件有问题。 On my server I have several folders, each of folders is separately website example below: 在我的服务器上,我有几个文件夹,每个文件夹分别是下面的网站示例:

Server structure (global folder) 服务器结构(全局文件夹)

- my_portfolio
- website_two
- my_website
- example

In "example" folder I have that .htaccess file: 在“示例”文件夹中,我具有该.htaccess文件:

Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ /$1.php [L]

#To remove www header
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [L,R=301]

RewriteRule working good when I open my website address is without "www" and .php extensions is invisible, but when I click on a link to other website in menu I get something like this: www.example.com/example/contact. 当我打开我的网站地址不带“ www”且.php扩展名不可见时,RewriteRule正常工作,但是当我单击菜单中其他网站的链接时,会得到类似以下内容:www.example.com/example/contact。

Address to my website is www.example.com and everything for this website is inside example folder on a server. 我网站的地址是www.example.com,此网站的所有内容都位于服务器上的example文件夹中。 I dont know where or what I wrote wrong. 我不知道我在哪里写错了什么。 Always when I try to go on to another webside rewrite engine give me a link without .php extension but add to folder name before link. 当我尝试使用另一个Webside重写引擎时,总是给我一个不带.php扩展名的链接,但在链接之前添加到文件夹名称。

This is good example: 这是一个很好的例子:

www.example.com/contact.php - normal link without htaccess
example.com/example(folder name where is a website)/contact - now
example.com/contact - should be

Reorder your rules and keep redirect rules before internal routing rules: 重新排列规则,并在内部路由规则之前保留重定向规则:

Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /

#To remove www header
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [L,R=301,NE]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NE]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L]

Also test this from a new browser to avoid old 301 cache. 同时从新的浏览器进行测试,以避免旧的301缓存。

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

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