将目录名称重写为 php 脚本名称 - 但允许结尾斜杠
Rewriting directory name to php script name - but allow ending slash
是的,我已经阅读了关于此的其他类似问题,这就是我想出这个可能的解决方案的方式:
# Turn on the mod_rewrite engine
RewriteEngine On
# IF the request filename with .php extension is a file which exists
RewriteCond %{REQUEST_FILENAME}.php -f
# AND the request is not for a directory
RewriteCond %{REQUEST_URI} !/$
# Redirect to the php script with the requested filename
RewriteRule (.*) \.php [L]
这可以通过重写请求来实现,例如:/about-us
到 about-us.php
但是如果你去 [=15] 它 不会 工作=].
我尝试了以下但没有用:
# Redirect to the php script with the requested filename
RewriteRule (.*)/? \.php [L]
跟第二个RewriteCond
有关系吗?
基本上,如果请求是针对真实文件或目录,我希望它失败(如果可能的话),但如果两者都不存在,那么无论请求是否有结尾斜线,它都应该重写为 PHP 脚本与否。
这可以做到吗?
你的规则是这样的:
当 .htaccess 和 .php 文件位于子目录中时,请使用:
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}/work/Client_Name/public_html/\.php -f [NC]
RewriteRule ^(.+?)/?$ .php [L]
当 .htaccess 和 .php 文件直接位于站点的根目录中时,请使用:
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}\.php -f [NC]
RewriteRule ^(.+?)/?$ .php [L]
是的,我已经阅读了关于此的其他类似问题,这就是我想出这个可能的解决方案的方式:
# Turn on the mod_rewrite engine
RewriteEngine On
# IF the request filename with .php extension is a file which exists
RewriteCond %{REQUEST_FILENAME}.php -f
# AND the request is not for a directory
RewriteCond %{REQUEST_URI} !/$
# Redirect to the php script with the requested filename
RewriteRule (.*) \.php [L]
这可以通过重写请求来实现,例如:/about-us
到 about-us.php
但是如果你去 [=15] 它 不会 工作=].
我尝试了以下但没有用:
# Redirect to the php script with the requested filename
RewriteRule (.*)/? \.php [L]
跟第二个RewriteCond
有关系吗?
基本上,如果请求是针对真实文件或目录,我希望它失败(如果可能的话),但如果两者都不存在,那么无论请求是否有结尾斜线,它都应该重写为 PHP 脚本与否。
这可以做到吗?
你的规则是这样的:
当 .htaccess 和 .php 文件位于子目录中时,请使用:
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}/work/Client_Name/public_html/\.php -f [NC]
RewriteRule ^(.+?)/?$ .php [L]
当 .htaccess 和 .php 文件直接位于站点的根目录中时,请使用:
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}\.php -f [NC]
RewriteRule ^(.+?)/?$ .php [L]