Regex 将目录名重写为php脚本名-但允许结束斜杠

Regex 将目录名重写为php脚本名-但允许结束斜杠,regex,apache,.htaccess,mod-rewrite,url-rewriting,Regex,Apache,.htaccess,Mod Rewrite,Url Rewriting,是的,我已经阅读了其他类似的问题,这就是我如何想出这个可能的解决方案: # 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 %

是的,我已经阅读了其他类似的问题,这就是我如何想出这个可能的解决方案:

# 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 (.*) $1\.php [L]
这可以通过重写请求来实现,例如:
/about-us
about-us.php
,但是如果您转到
/about-us/
,它就不起作用了

我尝试了以下方法,但没有成功:

# Redirect to the php script with the requested filename
RewriteRule (.*)/? $1\.php [L]
它是否与第二个重写条件有关

基本上,我希望它失败(如果可能的话),如果请求是一个真实的文件或目录,但如果两者都不存在,那么无论请求是否有结束斜杠,它都应该重写为PHP脚本


可以这样做吗?

将您的规则设置为:

当.htaccess和.php文件位于子目录中时,请使用:

RewriteEngine On 

RewriteCond %{DOCUMENT_ROOT}/work/Client_Name/public_html/$1\.php -f [NC] 
RewriteRule ^(.+?)/?$ $1.php [L]
RewriteEngine On 

RewriteCond %{DOCUMENT_ROOT}$1\.php -f [NC] 
RewriteRule ^(.+?)/?$ $1.php [L]
当.htaccess和.php文件直接位于站点的根目录下时,请使用:

RewriteEngine On 

RewriteCond %{DOCUMENT_ROOT}/work/Client_Name/public_html/$1\.php -f [NC] 
RewriteRule ^(.+?)/?$ $1.php [L]
RewriteEngine On 

RewriteCond %{DOCUMENT_ROOT}$1\.php -f [NC] 
RewriteRule ^(.+?)/?$ $1.php [L]

这可能是因为我在我的开发机器上的一个子目录下工作?嗯,更新的规则没有结束斜杠,但没有结束斜杠。它在名为
public\u html
的目录中的根目录下有3个目录。我们可以设置
RewriteBase
吗?是的,这就是为什么我想知道从
public\u html
public\u html
到“'web root'”的子目录路径。但是目录
public\uhtml
充当这个特定站点的根目录。然后,文件将位于
/public\u html/about us.php
,并通过
public\u html/about us/
请求。