Forum Moderators: phranque

Message Too Old, No Replies

redirect 301 x 800 or rewrite ?

         

BigDutch

11:17 pm on Sep 18, 2011 (gmt 0)

10+ Year Member



Hello,

I am asking for some assistance on .htaccess and i understand this is the right place.

I have a 'spidered' site built in joomla cms where the articles have been indexed with URL structure of www.domain.com/content/view/{LISTINGID}/{menuid}/

The new site i've been working on with another cms will have the same article at its SEF URL of www.domain.com/menu-name/{LISTINGID}

Now i understand that I could make a 800+ line long entry similar to :
Redirect 301 OLD http://www.domain.com/new
but i have been looking around for examples of this over the last 1-2 hours at using rewrite but have not been able to get anything more than the redirect 301 to work.

Can someone point me towards a tutorial or provide some suggestions ?

thanks

g1smd

11:26 pm on Sep 18, 2011 (gmt 0)

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



Use a single line RewriteRule with the RegEx pattern matching only URL requests that you need to be redirected.

Capture the parts of the request that you need to use again in a backreference. The target URL will also need to include the protocol and domain name. The rule will also use the [R=301,L] flags.



Actually, amended answer after reading the question again.

Since the target URL needs to contain "menu-name" and that information was not in the original URL request, you will need a completely different method to tackle this.

You will need to rewrite the requests to a PHP script that then "looks up" in the database what the new URL will be. The PHP script will then send the 301 redirect headers back to the browser.

If the list of IDs and names is fairly short (say, less than 100) and the list chnages infrequently you could load the data into a PHP array and do away with needing a database lookup from the PHP script.

BigDutch

11:48 pm on Sep 18, 2011 (gmt 0)

10+ Year Member



the 'menu-name' is the same across all the listing URLs that i would like to be redirected - does that steer me back to a single rewriterule ?

lucy24

11:58 pm on Sep 18, 2011 (gmt 0)

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



To clarify, because it can be read two ways:

Is "menu-name" your literal text, or do you mean {menuname} where the actual text is determined by the {menuid} value?

g1smd

12:05 am on Sep 19, 2011 (gmt 0)

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



If menu-name is always the same in the redirected-to URL, then you can probably use a single RewriteRule for all 800 redirects.

BigDutch

12:32 am on Sep 19, 2011 (gmt 0)

10+ Year Member



yes was a literal value - sorry for confusion. Ok, will look keep banging my head with the syntax of rewriterule.

lucy24

1:39 am on Sep 19, 2011 (gmt 0)

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



Bookmark this page if you haven't already done so ;)

[httpd.apache.org...]

The parts you especially need are rewrite_cond and rewrite_rule.

Note that Rules and Conditions work on a "two steps forward, one step back" principle. Apache doesn't even look at the rewrite_cond unless the rewrite_rule might apply. So for example:

Rewrite_cond %{HTTP_REFERER} {site-I-can't-stand}
Rewrite_rule foobar\.html {blahblah}

If the request being processed is not for a page called foobar.html, then mod_rewrite moves along without ever looking at the Condition. You could say

Rewrite_cond %{REQUEST_URI} foobar\.html
Rewrite_cond %{HTTP_REFERER} {site-I-can't-stand}
Rewrite_rule [a-z]+\.html {blahblah}

and it would "work", but it would waste a lot of server time* and resources because now it has to look up the Conditions every single time anyone requests a page.


* Those nanoseconds really add up.

g1smd

7:07 am on Sep 19, 2011 (gmt 0)

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



There's no underscore in RewriteCond or RewriteRule (and note the casing). :)

The basic redirect format is:
RewriteRule ^patterna/patternb/(patternc)/ http://www.example.com/newpath/$1 [R=301,L]

lucy24

8:30 am on Sep 19, 2011 (gmt 0)

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



Holy ###. Wonder what nether reaches of my brain those came from?* Let us stipulate that the cat stepped on the keyboard, and that what I thought I was typing was

Note that Rules and Conditions work on a "two steps forward, one step back" principle. Apache doesn't even look at the RewriteCond unless the RewriteRule might apply. So for example:

RewriteCond %{HTTP_REFERER} {site-I-can't-stand}
RewriteRule foobar\.html {blahblah}

If the request being processed is not for a page called foobar.html, then mod_rewrite moves along without ever looking at the Condition. You could say

RewriteCond %{REQUEST_URI} foobar\.html
RewriteCond %{HTTP_REFERER} {site-I-can't-stand}
RewriteRule [a-z]+\.html {blahblah}

and it would "work", but it would waste a lot of server time* and resources because now it has to look up the Conditions every single time anyone requests a page.



* Possibly from the name mod_rewrite, but that's grasping at straws ;)

g1smd

7:20 am on Sep 22, 2011 (gmt 0)

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



That's OK. I once filled a htaccess file with {%HTTP_HOST} and {%THE_REQUEST} (the "%" is in the wrong place) and wasted a whole hour wondering why I was getting Server Error 500 all the time.