Php 多重重写规则导致内部服务器错误
强迫这项工作让我有点疯狂,所以我希望你能帮助我 我使用重写规则和.htaccess创建动态URLPhp 多重重写规则导致内部服务器错误,php,apache,.htaccess,mod-rewrite,Php,Apache,.htaccess,Mod Rewrite,强迫这项工作让我有点疯狂,所以我希望你能帮助我 我使用重写规则和.htaccess创建动态URL example.com/page.php?id=1 像这样 example.com/1 使用 ,到目前为止,它工作得非常好。 但是我还想使用 因此,只要我不同时使用这两条规则,这两条规则都是完全正确的。当我这样做时,看起来像这样(我的完整文件) ,我得到一个内部服务器错误。我尝试了不同的版本,例如改变位置等等,但我总是会遇到这个错误 所以我的问题是:当URL结尾仍然隐藏且example.com
example.com/page.php?id=1
像这样
example.com/1
使用
,到目前为止,它工作得非常好。但是我还想使用 因此,只要我不同时使用这两条规则,这两条规则都是完全正确的。当我这样做时,看起来像这样(我的完整文件) ,我得到一个内部服务器错误。我尝试了不同的版本,例如改变位置等等,但我总是会遇到这个错误 所以我的问题是:当URL结尾仍然隐藏且
example.com/1
也起作用时,如何将这两个规则结合起来并发挥作用
非常感谢您的回答您可以在
.htaccess
文件中使用以下内容:
RewriteEngine on
# Check if the PHP file exists and route accordingly
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule (.*) $1.php [L]
# If not, pass the request to page.php if it contains A-Za-z0-9-
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([a-z0-9-]+)/?$ page.php?id=$1 [NC,L]
你需要两条单独的规则。重写条件将仅应用于紧跟其后的规则,并且使用您的php扩展规则,您必须在将php添加到末尾之前检查php文件是否存在:
RewriteEngine on
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.*)$ $1.php [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([A-Za-z0-9-]+)/?$ page.php?id=$1 [NC,L]
这是因为重写规则条件
(.*)
和([A-Za-z0-9-]+)
都将匹配URL中的/1
。服务器如何知道一个非真实文件和另一个文件之间的区别?
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) $1.php [L]
RewriteRule ^([A-Za-z0-9-]+)/?$ page.php?id=$1 [NC,L]
RewriteEngine on
# Check if the PHP file exists and route accordingly
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule (.*) $1.php [L]
# If not, pass the request to page.php if it contains A-Za-z0-9-
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([a-z0-9-]+)/?$ page.php?id=$1 [NC,L]
RewriteEngine on
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.*)$ $1.php [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([A-Za-z0-9-]+)/?$ page.php?id=$1 [NC,L]