.htaccess rewriterules和重定向

时间:2014-05-10 21:05:07

标签: php regex apache .htaccess mod-rewrite

对于域名市场,我想重写一些网址并将我的旧网页(在目录中)直接重定向到新网页。

对于“重写网址”,我使用了这些代码:

Options +FollowSymLinks -MultiViews
RewriteEngine On

RewriteCond %{REQUEST_URI} !^/(.*\.php)?$
RewriteCond %{REQUEST_URI} !^/(.*\.htm)?$
RewriteCond %{REQUEST_URI} !^/(.*\.html)?$
RewriteCond %{REQUEST_URI} !^/(.*\.shtml)?$
RewriteCond %{REQUEST_URI} !^/whois/$

RewriteRule ^(.*\.*)$ /details.php?domain=$1 [qsa,l]
RewriteRule ^whois/([^/]*)$ /whoisrequest/whois.php?whois=$1 [L]

例如http://mydomain.com/test.com url重写像http://mydomain.com/details.php?domain=test.com - 它很好。

问题:但与此同时,重写whois url(http://mydomain.com/whois/test.com)或将旧网页(如http://mydomain.com/domains/123/test.com)重定向到新网页(如http://mydomain.com/test.com })重写为…details.php?domain=…

我应该如何处理这些规则:

  • 如果http://mydomain.com/test.com,则重写http://mydomain.com/details.php?domain=test.com(仅在主网站上并包含“。”[dot] - 它可以像任何东西一样。)

  • 如果http://mydomain.com/folder1/或更多文件夹,则“。 (点)并不重要',不会重写。

  • 此外,将http://mydomain.com/domains/123/test.com重定向到http://mydomain.com/test.com

1 个答案:

答案 0 :(得分:1)

你有两个问题:

  1. 您在第一条规则中匹配非常通用的.*\..*模式,这将使第二条规则失效。
  2. 您没有使用RewriteCond %{REQUEST_FILENAME} !-f来避免重写真实文件/目录。
  3. 你的规则如下:

    Options +FollowSymLinks -MultiViews
    RewriteEngine On
    
    RewriteCond %{REQUEST_FILENAME} -f [OR]
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^ - [L]
    
    RewriteRule ^whois/([^/]+)/?$ /whoisrequest/whois.php?whois=$1 [L,QSA]
    
    RewriteRule ^([^/]+)/?$ /details.php?domain=$1 [L,QSA]