2015-04-01 199 views
1

基本上我想匹配文件路径下面的文件路径的任何模式,直到文件名,甚至文件没有扩展名。.htaccess正则表达式匹配文件扩展名或无扩展名

我几乎到了那里,但是一旦我尝试使它与没有扩展名的文件相匹配,它就会匹配到该行的其余部分。

我的测试字符串

http://regexr.com/foo.html?test (//regexr.com/foo.html) 

http://regexr.com/foo?test  (//regexr.com/foo) 

http://regexr.com/foo.php?test (//regexr.com/foo.htm) 

http://regexr.com/foo.htm?test (//regexr.com/foo.htm) 

我现在的表情是\/(.+)\.php但这并不没有扩展匹配第二个文件路径,如果我做了最后收集可选的,它选择通过GET参数。任何帮助将非常感激。


编辑下面是我试图修改

RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{THE_REQUEST} \ /(.+)(\.php|\.html?) 
RewriteRule^/%1.aspx [L,R=301] 
RewriteRule ^(.*).aspx$ $1.php [QSA] 

进一步编辑 正如指出的那样,这可能是更好的要求作为我的.htaccess块重定向问题。 (另外,我删除了html重定向,因为它会导致性能下降,所以扩展名为*.php的文件是唯一被重定向的文件。)

这些是我想如何处理这些文件; 301重定向到新的地址,然后继续被服务器处理为PHP这样:

http://regexr.com/foo.html (Doesn't redirect, not processed) 
http://regexr.com/foo  (R to /foo.aspx, processed as /foo.php) 
http://regexr.com/foo.php (R to /foo.aspx, processed as /foo.php) 
http://regexr.com/foo.htm (Doesn't redirect, not processed) 
http://regexr.com/foo.aspx (Doesn't redirect, processed as /foo.php) 
+0

你能显示你的规则吗? – anubhava 2015-04-01 15:05:02

+0

我可以,不知道这可能有什么帮助,但我会尽快更新我的答案 – 2015-04-01 15:07:54

回答

1

您可以使用此正则表达式:

\s/([^?.]+)(?:\.php|\.html?)?[?\s] 

RegEx Demo


重定向规则:

RewriteEngine On 

RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{THE_REQUEST} \s/([^?.]+)(?:\.php)?[?\s] [NC] 
RewriteRule^/%1.aspx? [L,R=302] 

RewriteRule ^(.+?)\.aspx$ $1.php [L,NC] 
+0

这仍然捕获GET参数,这是我试图避免 – 2015-04-01 15:27:13

+0

哦,这很容易,看到更新正则表达式 – anubhava 2015-04-01 15:34:04

+0

仍然无法得到这个工作,在我目前拥有'\ /(。+)(\。php | \ .html?)'的地方执行这个操作并不会发生任何重定向。另外,表达式还会选择其他扩展名的文件,导致重定向循环,因为它会选择'* .aspx'并将其重定向到自己 – 2015-04-01 15:47:41

相关问题