.htaccess无限循环用于不存在的url



我正在使用.htaccess来获得一个无扩展的站点。我已经在网上搜索并实现了一些有效的规则,但当我输入一个不存在的url(例如mysite.com/dsfadfasdf)时,它开始进入无限循环。

如果有人能帮我摆脱这个循环,也许可以重定向到我选择的页面(我会说这个页面不存在),我真的很感激!

Options +FollowSymLinks 
Options +Indexes 
RewriteEngine On 
RewriteBase /
# Redirect www to non www
RewriteCond %{HTTP_HOST} ^www.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
# Home page language
RewriteRule ^(en|ro)/?$ index.php?lang=$1 [L]
# For the profile page
RewriteRule ^(.+)/accommodation-(.+)/([0-9]+)/(.+)$ location.php?id=$3&town=$2&hname=$4&lang=$1 [NC,L]
# Internally rewrite extensionless URL to corresponding .php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^(.*)$ $1.php?%1 [NC,L,QSA]
# Externally redirect (only) direct client requests for .php URLs to extensionless URLs:
RewriteCond %{THE_REQUEST} ^[A-Z]+ /([^/]+/)*[^.#? ]+.php([#?][^ ]*)? HTTP/
RewriteRule ^(([^/]+/)*[^.]+).php http://mysite.com/$1 [R=301,L] 

我认为这个问题属于最后一条规则。有什么建议吗?

.htaccess会在url发生更改时继续重写。使用END标志在内部重写url后,强制其停止重写:

# Internally rewrite extensionless URL to corresponding .php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^(.*)$ $1.php?%1 [NC,END,QSA]

文档在这里。

最新更新