【问题标题】:.htaccess rewriterules and redirections.htaccess 重写规则和重定向
【发布时间】:2014-05-10 21:05:07
【问题描述】:

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

对于“rewrite url”,我使用了这些代码:

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)重写为@987654327 @

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

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

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

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

【问题讨论】:

  • 我认为RewriteCond 只会在第一个RewriteRule 中使用。为每个RewriteRule添加这些
  • 感谢您的帮助。我将编辑我的文件。

标签: php regex apache .htaccess mod-rewrite


【解决方案1】:

你有两个问题:

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

有这样的规则:

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]

【讨论】:

  • 非常感谢阿努巴瓦!您的智能代码完美运行!
  • 如何重定向我的旧页面?实际上我的旧网址也在重写网址。例如,我使用了以下代码:'RewriteRule ^domains/([a-z0-9]+)/(.*)/? details.php?id=$1 [NC,L]' 所以,mydomain.com/domains/123/test.com url 重写为 mydomain.com/details.php?id=123 但是现在,我使用域名而不是 ids 用于详细信息页面。我想用我的新 htaccess 代码将 301 mydomain.com/domains/123/test.com 重定向到 mydomain.com/test.com。谢谢
  • 对于这个新的重定向,您可以使用:RewriteRule ^domains/[a-z0-9]+/(.+)/?$ /$1 [NC,L,R]
  • 感谢您的帮助 anubhava。如果您接受,我想通过 paypal 向您付款
  • 感谢您的提议,但我想继续花时间在 SO 上帮助社区中的其他开发人员。多次使用各种开源软件让我受益匪浅,这是我回馈社区的方式。
猜你喜欢
  • 2020-11-01
  • 2016-06-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-22
  • 2013-01-21
  • 1970-01-01
相关资源
最近更新 更多