开发者

mod_Rewrite conditions and rules ( confused ) help please

开发者 https://www.devze.com 2023-02-25 13:14 出处:网络
Help please, After numerous hours searching for the solution to my problem i have become completely confused, so my appologies if this as been answered before.

Help please,

After numerous hours searching for the solution to my problem i have become completely confused, so my appologies if this as been answered before.

I have a current URL as so :

http://www.mydomain.com/news/dentistry_dental/article_detail.php?article=2968&title=redefining-oral-hygiene-intervention

I am cleaning up my urls so have now created a rewrite rule to parse the following to the previous url :

http://www.mydomain.com/dental_news/2968/Redefining-oral-hygiene-intervention

The rule i have used in the .htaccess file for this is as below :

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

This is working as expected, however.. I need to also have the OLD url ( That is indexed on many Search Engines ) to now point to the NEW url with a 301 redirect.

I have tried numerous things but as of yet i get nothing but errors, i presume its a looping error i have.

Does anyone out there have any idea how i can achieve the 2 way redirect without looping?

Any help will be greatly appreciated

EDIT

The above problem has been resolved, as you can see by the answer below, however i now have another query, this should help me in understanding whats going on i hope开发者_开发知识库 :)

I have another URL www.mydomain.com/news/dentistry_dental/index.php that i want to redirect with 301 to www.mydomain.com/dental_news/

I have tried rewriting the code from the answer below to do this but it was a fail, i am still lacking understanding of the whole thing i think.

Any help be great :)

Regards M


Try the following. The first condition should protect you from a redirect loop (it only matches requests not beginning with /dental_news. The %1 and %2 will match the corresponding regex groups from the previous RewriteCond. Also, the trailing ? on the RewriteRule, will remove the querystring on redirect.

RewriteCond %{REQUEST_URI} !^\/dental_news\/
RewriteCond %{QUERY_STRING} article=([0-9]*)&title=(.*)$
RewriteRule (.*) /dental_news/%1/%2? [L,R=301]

Hope this helps.

EDIT: I changed the last group of the second RewriteCond to match up until the $ of $fr=1, if this is intended to be &1, you can change the end of the RewriteCond to group ([^&]*) instead.

RewriteCond %{REQUEST_URI} !^\/dental_news\/
RewriteCond %{QUERY_STRING} article=([0-9]*)&title=([^$]*)$
RewriteRule (.*) /dental_news/%1/%2? [L,R=301]

EDIT:

RewriteEngine On

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

RewriteCond %{REQUEST_URI} !^/dental_news/
RewriteCond %{QUERY_STRING} article=([0-9]*)&title=([^&]*)$
RewriteRule (.*) /dental_news/%1/%2? [L,R=301]
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号