Ahmet
Ahmet

Reputation: 43

301 redirect error dynamic and static index.php?

I have a my basic links to my new site.

RewriteCond %{QUERY_STRING} id=1
RewriteRule ^index\.php$ https://www.newdomain.com/about? [L,R=301]

RewriteCond %{QUERY_STRING} id=2
RewriteRule ^index\.php$ https://www.newdomain.com/contact? [L,R=301]

But I can not get the correct orientation for / and /index.php. Because if you do the following, the other directions do not work.

redirect 301 /index.php https://www.newdomain.com/
redirect 301 / https://www.newdomain.com/

this is the result of editing for index.php:

/ https://www.newdomain.com/
/index.php https://www.newdomain.com/index.php
/index.php?id=1 https://www.newdomain.com/index.php?id=1

How can I fix?

Upvotes: 3

Views: 89

Answers (1)

MrWhite
MrWhite

Reputation: 45914

You shouldn't mix mod_rewrite (RewriteRule) and mod_alias (Redirect) directives. You are likely to get conflicts - which is probably what's happening here. Different Apache modules execute at different times during the request, despite the apparent order in the config file. mod_rewrite executes first, but Redirect works on the original request and does not see the rewritten request by mod_rewrite.

You need to change the Redirect directives to mod_rewrite RewriteRule directives, and arrange the rules so that the most specific are first.

For example:

RewriteCond %{QUERY_STRING} id=1
RewriteRule ^index\.php$ https://www.newdomain.com/about? [L,R=301]

RewriteCond %{QUERY_STRING} id=2
RewriteRule ^index\.php$ https://www.newdomain.com/contact? [L,R=301]

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^index\.php$ https://www.newdomain.com/ [R=301,L]

You will need to clear your browser cache before testing. (Often easier to test with 302 - temporary - redirects for this reason.)

However, it's not entirely clear what your redirects are supposed to be doing? redirect 301 / https://www.newdomain.com/ would create a redirect loop unless newdomain.com is an entirely different site? But if it is then there are probably better ways to redirect traffic?

Upvotes: 1

Related Questions