Web.config:使用尾部斜杠将URL输入重定向到URL而不使用尾部斜杠

问题描述 投票:1回答:1

我继承了一个在IIS 7.5上运行的网站PHP网站。

如果我去:https://www.example.com/page没有错误,它会按预期显示page.php内容。

如果我去:https://www.example.com/page/我收到404未找到错误。我想要重定向/页面并显示page.php内容。

我相信这是因为服务器正在寻找文件页面/ .php(或page.php / ??),但我需要一些帮助来解决在web.config中放置什么代码来解决这个问题。

php web-config iis-7.5 trailing-slash
1个回答
0
投票

如果您使用iis,请在web.config中添加此项以始终从URL中删除尾部斜杠:

<rule name="Remove trailing slash" stopProcessing="true">  
<match url="(.*)/$" />  
<conditions>  
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />  
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />  
</conditions>  
<action type="Redirect" redirectType="Permanent" url="{R:1}" />  
</rule> 

如果你有一个Apache Web服务器,那么在.htaccess中添加:

 RewriteEngine on
 #unless directory, remove trailing slash
 RewriteCond %{REQUEST_FILENAME} !-d
 RewriteRule ^(.*)/$ /$1 [R=301,L]

 #resolve .php file for extensionless php urls
 RewriteCond %{REQUEST_FILENAME} !-d
 RewriteCond %{REQUEST_FILENAME}\.php -f
 RewriteRule ^(.*)$ $1.php [L]

 #redirect external .php requests to extensionless url
 RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*[^.#?\ ]+\.php([#?][^\ ]*)?\ HTTP/
 RewriteRule ^(([^/]+/)*[^.]+)\.php /$1 [R=301,L]
© www.soinside.com 2019 - 2024. All rights reserved.