1

I have products.php which I'm trying to change into /europe-cell-phone/. I've made a rewrite rule to do this:

RewriteRule ^europe-cell-phone/$ products.php [L]

However I also need to 301 the old page (products.php) to /europe-cell-phone/. I'm trying to do this with the following rule:

RewriteRule ^products.php$ http://www.eurobuzz.com/europe-cell-phone/ [R=301,L]

However of course when I go to products.php expecting to be redirected I hit an infinite redirect loop, is there any solution to this? Never encountered this problem before.

Dennis Williamson
  • 62,149
  • 16
  • 116
  • 151
zuk1
  • 141
  • 1
  • 12

3 Answers3

2

You need to test the original request in THE_REQUEST as REQUEST_URI already might have been changed by a rule:

RewriteCond %{THE_REQUEST} ^[A-Z]+\ /products\.php
RewriteRule ^products\.php$ /europe-cell-phone/ [R=301,L]
RewriteRule ^europe-cell-phone/$ products.php [L]
Gumbo
  • 436
  • 2
  • 6
1

You need to rename your products.php to products-new.php and create two rules like these:

RewriteRule ^europe-cell-phone/$ products-new.php [L]

RewriteRule ^products.php$ products-new.php [R=301,L]

SergeyZh
  • 194
  • 2
  • not the best solution but oh well. – zuk1 Sep 18 '09 at 11:01
  • Don’t forget to escape the dot. – Gumbo Sep 18 '09 at 12:31
  • The second (external redirect) directive would need a slash prefix, or use an absolute URL, or set `RewriteBase /` - otherwise you'll get a malformed redirect. But there is no need to create a `products-new.php` file - see [@Gumbo's solution](https://serverfault.com/a/66664/368326) – DocRoot May 16 '19 at 22:47
0

for rewrite in apache, you write them like this: RewriteRule pattern substitution

So the first rule you list matches "europe-cell-phone" and replaces it with "products.php"

Your second rule seems to do the opposite. I think you may need to switch things around in your first rule.

brad.lane
  • 461
  • 4
  • 13
  • My first rule means that when I go to domain.com/europe-cell-phone/ it produces the page products.php. – zuk1 Sep 17 '09 at 15:33
  • (which is what I want) – zuk1 Sep 17 '09 at 15:34
  • sorry, i'm confused. is URL to products.php domain.com/europe-cell-phone/products.php, or is it domain.com/products.php? – brad.lane Sep 17 '09 at 15:45
  • I want domain.com/europe-cell-phone/ to display domain.com/products.php, and I want people who go to domain.com/procucts.php to go to domain.com/europe-cell-phone/ – zuk1 Sep 17 '09 at 15:50
  • make your second rule more specific. when your second rule gets rewritten by apache, it triggers your first, which triggers your second, etc, etc. include domain.com in the pattern of your second rule; not just ^products.php$ – brad.lane Sep 17 '09 at 16:07