【问题标题】:.htaccess mod_rewrite 301 redirect with a pattern.htaccess mod_rewrite 301 使用模式重定向
【发布时间】:2011-12-31 06:26:58
【问题描述】:

我已更改我的 URL 结构以从项目页面的 URL 中删除类别。

我需要某种方式将所有旧页面重定向到新页面,使用 mod_rewrite。我已经有一个相当复杂和凌乱的 .htaccess 文件。

我需要的是重定向: http://example.com/category-name/item-name/这个http://example.com/download-item-name/

现在,我在 htaccess 文件中找到了这个。

RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} !^example.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301]
RewriteRule ^download-([^/]*)/screenshots/$ /screenshots.php?screenshots=$1 [L]
RewriteRule ^download-([^/]*)/screenshots/([^/]*)/$ /screenshots.php?screenshots=$1&shot=$2
#and now without trailing slash, so request will hit script which can do redirect.
RewriteRule ^download-([^/]*)/screenshots/([^/]*)$ /screenshots.php?screenshots=$1&shot=$2 [L]
RewriteRule ^feed/([^/]*)/$ /feed/?category=$1 [L]
RewriteRule ^download-([^/]*)-software/$ /category.php?category=$1 [L] 
#this needs to stay above the below rule
RewriteRule ^download-([^/]*)/$ /software.php?shortname=$1 [QSA]

RewriteRule ^download-([^/]*)/([^/]*)/$ /software.php?shortname=$1&lang=$2 [QSA]

#and now without trailing slash, so request will hit script which can do redirect.
RewriteRule ^download-([^/]*)$ /software.php?shortname=$1 [QSA]

RewriteRule ^download-([^/]*)/([^/]*)$ /software.php?shortname=$1&lang=$2

RewriteRule ^downloading/([^/]*)/$ /download.php?downloading=$1
RewriteRule ^downloading/([^/]*)/([^/]*)/$ /download.php?downloading=$1&lang=$2 [L]
RewriteRule ^download/([^/]*)/([^/]*)/$ /send.php?key=$1&s=$2 [QSA]
RewriteRule ^FQ-([^/]*)/$ /content.php?page=$1 [L]
RewriteCond %{REQUEST_URI} ^(/[^/]+)(/.*)?$
RewriteCond %{DOCUMENT_ROOT}/categories%1/ -d
RewriteRule . /categories%{REQUEST_URI} [QSA]
Options -Indexes
FileETag None
ErrorDocument 404 http://example.com/404/
ErrorDocument 403 http://example.com/

#Some redirects
Redirect 301 /audio-video-players-codecs/zoom-player-max/ http://example.com/download-zoom-player-max/
Redirect 301 /audio-video-players-codecs/zoom-player-premium/ http://example.com/download-zoom-player-premium/
Redirect 301 /audio-video-players-codecs/zoom-player-pro/ http://example.com/download-zoom-player-pro/

【问题讨论】:

    标签: .htaccess url mod-rewrite


    【解决方案1】:

    你的 .htaccess 很乱,因为很简单:

    规则 #1: 避免混合使用 RedirectRewriteRules 指令。这只会使事情变得混乱。 总是有可能将Redirect 替换为RewriteRules 指令。

    规则#2:如果你不需要RewriteBase /(通常是这样,因为基数通常是/),不要写它。这只会使事情变得混乱。越短越好(这是她告诉我的(开玩笑的)。

    规则 #3:^(.*)$(.*) 相同。在这里,这也让事情变得更清楚了。

    规则#4::为您准备的特殊规则:您可以使用 ^downloading/([^/]*)(/([^/]*))?/ 之类的东西将几乎所有的 RewriteRules 减少 2,这会使第二个参数为空,但同时匹配一个或两个参数 (只是在你的 PHP 中有效,你可以获得一个空的第二个参数)。越短越好。

    然后回答你的问题:重定向:http://example.com/category-name/item-name/ 到这个http://example.com/download-item-name/

    RewriteRule ^category-([a-zA-Z]+)/item-([a-zA-Z]+)/ ^download-item-$2/ [QSA,R]
    

    这应该可以工作


    我最喜欢的检查正则表达式的工具:

    http://www.quanetic.com/Regex(别忘了选择 ereg(POSIX) 而不是 preg(PCRE)!)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-09-03
      • 1970-01-01
      • 2013-09-10
      • 2012-08-18
      • 2011-06-18
      • 1970-01-01
      • 2011-04-29
      相关资源
      最近更新 更多