Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/apache/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 多重重写规则导致内部服务器错误_Php_Apache_.htaccess_Mod Rewrite - Fatal编程技术网

Php 多重重写规则导致内部服务器错误

Php 多重重写规则导致内部服务器错误,php,apache,.htaccess,mod-rewrite,Php,Apache,.htaccess,Mod Rewrite,强迫这项工作让我有点疯狂,所以我希望你能帮助我 我使用重写规则和.htaccess创建动态URL example.com/page.php?id=1 像这样 example.com/1 使用 ,到目前为止,它工作得非常好。 但是我还想使用 因此,只要我不同时使用这两条规则,这两条规则都是完全正确的。当我这样做时,看起来像这样(我的完整文件) ,我得到一个内部服务器错误。我尝试了不同的版本,例如改变位置等等,但我总是会遇到这个错误 所以我的问题是:当URL结尾仍然隐藏且example.com

强迫这项工作让我有点疯狂,所以我希望你能帮助我

我使用重写规则和.htaccess创建动态URL

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]