mod重定向和重写(第2部分)

时间:2011-04-12 19:26:29

标签: apache mod-rewrite url-rewriting

继我之前的问题之后,我让自己变得一团糟,所以我会尝试尽可能地解决我的问题。

我正在整理我的网址结构,但问题是我的网址在搜索引擎中编入索引,所以我需要重写我的网址,同时重定向它们。

在前一个问题的帮助下,我目前在.htaccess中有以下内容

Options +FollowSymlinks
RewriteEngine on

rewritecond %{http_host} ^mydomain.com [nc]
rewriterule ^(.*)$ http://www.mydomain.com/$1 [r=301,nc]

# Rewrite all index.php to root: / ( with perm redirect )
RewriteCond %{THE_REQUEST} ^.*/index.php
RewriteRule ^(.*)index.php$ http://www.mydomain.com/$1 [R=301]

# Rewrite A newly added section rewrite ( no redirect required )
RewriteRule ^products/digital_imaging/([^/]*)/([^/]*)$ /products/digital_xray.php?id=$1&product=$2 [L]

# Rewrite dental news article to neat nice url
RewriteRule ^dental_news/([^/]*)/([^/]*)$ news/dentistry_dental/article_detail.php?article=$1&title=$2&redirect [L]

#Conditional rewrite of old news article path to new one with 301 redirect
RewriteCond %{REQUEST_URI} !^/dental_news/
RewriteCond %{QUERY_STRING} article=([0-9]*)&title=([^&]*)$
RewriteRule (.*) /dental_news/%1/%2? [L,R=301]

所以,这一切都按预期工作,但我有一个以index.php结尾的另一个url,有时但并不总是有php变量,这个我还需要重写和重定向,通过尝试自己做我自己造成某种冲突,而不是按预期工作。

我需要重写的网址& 301的重定向低于

http://www.mydomain.com/news/dentistry_dental/index.php?month=February&year=2011

With the previous index.php rule it currently displays as

http://www.mydomain.com/news/dentistry_dental/?month=February&year=2011

Half way there i guess... I need it to be the following

http://www.mydomain.com/dental_news/February/2011

请注意,网址有时没有变量,如下所示

http://www.mydomain.com/news/dentistry_dental/index.php

Which is currently with the index.php rule above is showing as

http://www.mydomain.com/news/dentistry_dental/

This needs to be

http://www.mydomain.com/dental_news/

所以是的,我完全不喜欢这个东西,这对我来说是全新的,所以如果我能把这个排序好的话确实是一个非常快乐的兔子!

全部谢谢

修改

好的,我现在的.htaccess看起来如下。

Options +FollowSymlinks
RewriteEngine on

rewritecond %{http_host} ^mydomain.com [nc]
rewriterule ^(.*)$ http://www.mydomain.com/$1 [r=301,nc]

# Rewrite all index.php to root: / ( with perm redirect )
RewriteCond %{THE_REQUEST} ^.*/index.php
RewriteRule ^(.*)index.php$ http://www.mydomain.com/$1 [R=301]

RewriteRule ^products/digital_imaging/([^/]*)/([^/]*)$ /products/digital_xray.php?id=$1&product=$2 [L]

# Rewrite dental news article to neat nice url
RewriteRule ^dental_news/([^/]*)/([^/]*)$ news/dentistry_dental/article_detail.php?article=$1&title=$2&redirect [L]

#Conditional rewrite of old news article path to new one with 301 redirect
RewriteCond %{REQUEST_URI} !^/dental_news/
RewriteCond %{QUERY_STRING} article=([0-9]*)&title=([^&]*)$
RewriteRule (.*) /dental_news/%1/%2? [L,R=301]

RewriteCond %{QUERY_STRING} month=([^&]*)&year=([^&]*)
RewriteRule (.*) /dental_news/%1/%2? [R=301]
RewriteRule news/dentistry_dental/$ /dental_news/ [R=301]
RewriteRule ^dental_news/$ /news/dentistry_dental/index.php [L]

就此而言,发生以下情况

www.mydomain.com/news/dentistry_dental/index.php 
-> www.mydomain.com/dental_news/ 

(^正确)

www.mydomain.com/news/dentistry_dental/index.php?month=May&year=2011 
-> www.mydomain.com/dental_news/April/2011 

(^错误:在网址栏中显示正确的路径,但显示article_detail.php而不是index.php和变量)

我猜这两个规则是相似的,一个是拿起年份和月份变量的index.php并发送到article_detail页面。然而,我可能错了,我不知道如何解决。

编辑#2

目前.htaccess如下。

Options +FollowSymlinks
RewriteEngine on

rewritecond %{http_host} ^dentalsupportuk.com [nc]
rewriterule ^(.*)$ http://www.dentalsupportuk.com/$1 [r=301,nc]

# Rewrite all index.php to root: / ( with perm redirect )
RewriteCond %{THE_REQUEST} ^.*/index.php
RewriteRule ^(.*)index.php$ http://www.dentalsupportuk.com/$1 [R=301]

RewriteRule ^products/digital_imaging/([^/]*)/([^/]*)$ /products/digital_xray.php?id=$1&product=$2 [L]

# Rewrite dental news article to neat nice url
RewriteRule ^dental_news/([0-9]*)/([^/]*)$ news/dentistry_dental/article_detail.php?article=$1&title=$2&redirect [L]

#Conditional rewrite of old news article path to new one with 301 redirect
RewriteCond %{REQUEST_URI} !^/dental_news/
RewriteCond %{QUERY_STRING} article=([0-9]*)&title=([^&]*)$
RewriteRule (.*) /dental_news/%1/%2? [L,R=301]

RewriteCond %{QUERY_STRING} month=([^&]*)&year=([^&]*)
RewriteRule (.*) /dental_news/%1/%2? [R=301]
RewriteRule news/dentistry_dental/$ /dental_news/ [R=301]
RewriteRule ^dental_news/([a-zA-Z]*)/([0-9]*)$ news/dentistry_dental/index.php?month=$1&year=$2&redirect [L]

www.mydomain.com/dental_news/现在重写到www.mydomain.com/dental_news/,但不存在,因此404错误(应该重写为www.mydomain.com/news/dentistry_dental /)

www.mydomain.com/dental_news/100/some-title现在重写正如所期望的那样www.mydomain.com/news/dentistry_dental/article_detail.php与变量(工作)

www.mydomain.com/news/dentistry_dental/article_detail.php?article=100&title=some-title 重定向如预期的那样www.mydomain.com/dental_news/100/some-标题和正确显示(工作)

www.mydomain.com/news/dentistry_dental/index.php?month=May&year=2010 重定向但崩溃了Firefox说明(Firefox检测到服务器正在重定向请求这个地址永远不会完成。)(不工作,这是某种循环造成这种情况吗?)

www.mydomain.com/dental_news/July/2010/ 重写给了我404错误。

所以是的,我到处都是,也许我已经把错误的顺序或东西,但我越乱越害怕打破一切。有什么想法吗?

此致 中号

1 个答案:

答案 0 :(得分:0)

尝试添加以下两条规则:

RewriteCond %{QUERY_STRING} month=([^&]*)&year=([^&]*)
RewriteRule (.*) /dental_news/%1/%2? [R=301]

RewriteRule news/dentistry_dental/$ /dental_news/ [R=301]

我认为它不会导致任何冲突,并且似乎涵盖了您描述的两种情况。

修改

好的,所以如果我说得对,那么/dental_news/2968/Redefining-oral-hygiene-intervention的网址应该用适当的查询字符串重写为/news/dentistry_dental/article_detail.php。然后/dental_news/April/2011应该重写为/news/dentistry_dental/index.php。所以...如果是这种情况,您应该能够添加以下规则:

RewriteRule ^dental_news/([a-zA-Z]*)/([0-9]*)$ news/dentistry_dental/index.php?month=$1&year=$2&redirect [L]

进行以下更新可能也是有意义的:

目前:RewriteRule ^dental_news/([^/]*)/([^/]*)$ news/dentistry_dental/article_detail.php?article=$1&title=$2&redirect [L]

更新:RewriteRule ^dental_news/([0-9]*)/([^/]*)$ news/dentistry_dental/article_detail.php?article=$1&title=$2&redirect [L]

区别在于它更具体,第一个正则表达式组只匹配一组数字,有助于避免与重写index.php文件的规则发生冲突。

编辑2 我相信我测试了您提供的所有网址,现在它们似乎正在运行。我们没有考虑过几个重定向循环。希望这会有所帮助...

Options +FollowSymlinks
RewriteEngine on

rewritecond %{http_host} ^mydomain.com [nc]
rewriterule ^(.*)$ http://www.mydomain.com/$1 [r=301,nc]

# Rewrite all index.php to root: / ( with perm redirect )
RewriteCond %{THE_REQUEST} ^.*/index.php
RewriteRule ^(.*)index.php$ http://www.mydomain.com/$1 [R=301]

RewriteRule ^products/digital_imaging/([^/]*)/([^/]*)$ /products/digital_xray.php?id=$1&product=$2 [L]

RewriteRule dental_news/$ /news/dentistry_dental/?rewrite [L]

# Rewrite dental news article to neat nice url
# Protect from looping because of previous rules
RewriteCond %{QUERY_STRING} !rewrite 
RewriteRule ^dental_news/([0-9]*)/([^/]*)$ news/dentistry_dental/article_detail.php?article=$1&title=$2&rewrite [L]

#Conditional rewrite of old news article path to new one with 301 redirect
RewriteCond %{REQUEST_URI} !^/dental_news/
RewriteCond %{QUERY_STRING} article=([0-9]*)&title=([^&]*)$
RewriteRule (.*) /dental_news/%1/%2? [L,R=301]

RewriteCond %{REQUEST_URI} !^/dental_news/
RewriteCond %{QUERY_STRING} month=([^&]*)&year=([^&]*)$
RewriteRule (.*) /dental_news/%1/%2? [R=301]


# Protect from looping because of previous rules
RewriteCond %{QUERY_STRING} !rewrite 
RewriteRule news/dentistry_dental/$ /dental_news/ [R=301]

# Protect from looping because of previous rules
RewriteCond %{QUERY_STRING} !rewrite 
RewriteRule ^dental_news/([a-zA-Z]*)/([0-9]*)/?$ news/dentistry_dental/index.php?month=$1&year=$2&rewrite [L]

希望这有帮助。