Forum Moderators: phranque

Message Too Old, No Replies

Redirect Dynamic URL to Static

Almost there, but not quite

         

AWebHaus

1:17 am on Nov 11, 2011 (gmt 0)

10+ Year Member



I want to redirect some dynamic url's to static ones of my choosing, example;


index.php?lang=nl&p=wie
index.php?lang=nl&p=wat
index.php?lang=nl&p=blat


to


wie/super
wat/trinidad
blat


Using


<IfModule mod_rewrite.c>
RewriteRule ^wie/super$ index.php?lang=nl&p=wie
RewriteRule ^wat/trinidad$ index.php?lang=nl&p=wat
RewriteRule ^blat$ index.php?lang=nl&p=blat
</IfModule>


The links wie/super etc. already work, but redirecting seems to be a real pain in the ass. I tried several things, but I get either a redirect loop, or it redirects to the dynamic one, or it redirect to the static one but doesn't take the parameters into account and the static url gives a 404.

I'm sure my problem is really silly/stupid, but after 3 hours of searching the net and this forum, I didn't come up with the solution.

Any pointers on where to start with this?

Thank you for your time,
Fabien

lucy24

5:13 am on Nov 11, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Yes, you need to start by getting a firm grip on the difference between Redirect and Rewrite. Both can be done via mod_rewrite, so that's not an issue. There are, at a rough estimate, 10,000 previous posts in this Forum explaining the difference-- or you can go study the Apache documentation-- so there's no need to repeat that part.

You can also get rid of the <IfModule bits. Those are for generic htaccess files, where the designer doesn't know who will be using it or what their setup is. You either have mod_rewrite or you don't, so work accordingly. (Although frankly I have a hard time imagining an Apache installation that doesn't have mod_rewrite.) Make sure you have the line

RewriteEngine On


before any of your Rewrites. You only need to say it once. (Help! g1 or someone like him! Is that once per htaccess, or once per path?)

As currently written, your RewriteRules have these obvious* problems:

#1 They do the exact opposite of what you start out saying (in English) that you want to do: they rewrite (not redirect) to a dynamic url.

#2 They are missing the obligatory [L] flag-- must always be included unless there is a specific reason to omit it

#3 They are unambiguous Rewrites, which may or may not be what you want. If instead you want Redirects, that requires a [R=301] flag.

To take one at random:

RewriteRule ^blat$ index.php?lang=nl&p=blat


means: the user (or an earlier Redirect) asks for, or clicks on, the exact name

www.example.com/blat


(with or without query) and you instead serve them content from

www.example.com/index.php?lang=nl&p=blat


where the query "lang=nl&p=blat" replaces the previous query, if any.


* "Obvious" in this context means that I noticed them right away, even though I don't speak fluent Apache.

wilderness

7:08 am on Nov 11, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



once per htaccess

g1smd

7:10 am on Nov 11, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You've shown the code for the rewrite part, and that's the easy bit of the problem. Add the [L] flag to every rule.

Your redirects should use a RewriteRule, but to avoid the loop you will need to test THE_REQUEST with a preceding RewriteCond.

A forum search for "redirect RewriteCond THE_REQUEST" will find thousands of code examples to study.