Help with mod_rewrite and mod_redirect

2020-05-01 08:55发布

My .htaccess file is:

Redirect 301    http://domain.com/news/articles?dtMain_start=150    http://domain.com/news/articles
Redirect 301    http://domain.com/news/articles?dtMain_start=160    http://domain.com/news/articles
Redirect 301    http://domain.com/news/articles?dtMain_start=170    http://domain.com/news/articles
# 
RewriteEngine On
RewriteBase /

# The Friendly URLs part
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]

I also have to incorporate the following rule

RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain.com
RewriteRule (.*) http://www.domain.co.uk/$1 [R=301,L]

I cannot get them to work together... can anyone help...

I tried just stacking the Redirects before the RewriteCond and I get this... http://www.domain.com/news/articles?q=news/articles?dbMain_start=150
ie http://domain.com/newpage?q=oldpage

Okay Mod_Alias and Mod_Rewrite don't like each other.

Can I write something like:

RewriteCond %{REQUEST_QUERY_STRING} ^.*&bodgeredirect=true$
RewriteRule ^(.*)&bodgeredirect=true$ index.php?q=$1 [L,QSA]

1条回答
唯我独甜
2楼-- · 2020-05-01 09:34

First of all: There is not mod_redirect. Redirect is a directive of mod_alias.

And the Redirect directive, like any other directive of mod_alias, does only work with the URL path. So your Redirect directives won’t work as expected. Use mod_rewrite equivalents instead:

RewriteCond %{HTTP_HOST} =example.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

RewriteCond %{QUERY_STRING} ^dtMain_start=(150|160|170)$
RewriteRule ^news/articles$ /news/articles? [L,R=301]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]

In general it is not a good idea to mix mod_alias and mod_rewrite if the patterns coincide with each other.

查看更多
登录 后发表回答