Forum Moderators: phranque

Message Too Old, No Replies

Redirecting multiple dynamic pages

         

Enigma5

4:01 pm on Oct 30, 2009 (gmt 0)

10+ Year Member



I am trying to redirect many pages on my site. The syntax for the pages is this: http://www.example.org/index.php?x=reviews&id=920

Now I want to redirect certain pages to the /old directory so the above URL would be: http://www.example.org/old/index.php?x=reviews&id=920

I only want to select reviews 200-920, this is currently what the .htaccess looks like:

RewriteCond %{QUERY_STRING} ^x=reviews&id=[200-920]?
RewriteRule ^index.php$ http://www.example.org/old/index.php [R=301]

this makes everything that has the "x=reviews&id=" string in it go to the /old/ directory not the pages with id's 200-920...any suggestions?

[edited by: jdMorgan at 4:53 pm (utc) on Oct. 30, 2009]
[edit reason] example.org. Please see TOS. [/edit]

jdMorgan

4:52 pm on Oct 30, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> x=reviews&id=[200-920]?

This evaluates to "Match a single digit '2', '0', '0 through 9', '2', or '0', or totally blank." Then since the pattern is un-anchored, any characters which follow this sub-pattern are also matched by default.

Remember that regular-expressions work on characters and strings and do not "comprehend" numeric values at all; everything is treated as a character or a string of characters -- i.e. "symbols", and regex does not assign any "meaning" to the matched characters or strings.

I believe that what you want is more like:


RewriteCond %{QUERY_STRING} ^x=reviews&id=([1-9]?[0-9]¦[1-8][0-9][0-9]¦9[01][0-9]¦920)(&.*)?$
RewriteRule ^index.php$ http://www.example.org/old/index.php [R=301,L]

The "id=" subpatterns match 0-9 and 10-99, 100-899, 900-919, and 920, respectively. These id digits may be followed by additional query string parameters starting with "&", but if present, they are ignored for the purposes of pattern-matching.

The rule, as is usually required and recommended, is terminated with an [L] flag.

Replace the broken pipe "¦" characters above with solid pipe characters before use; Posting on this forum modifies the pipe characters.

Also, we generally recommend using "/" as the index page at any subdirectory level, assigning the index.whatever file as the index page filepath using the DirectoryIndex directive.

Having done that, we generally recommend linking and/or redirecting always to "/" and never to "index.whatever", as there is no need or advantage in 'exposing' your server-side technology to users and search engines, and doing so means that should you ever change that technology, you will have to redirect your index pages, suffering the attendant (usually temporary, but often long-lived) ranking loss as a result.

Jim

jdMorgan

4:57 pm on Oct 30, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



A further thought: If you are changing these URLs simply because you have moved these files (or wish to move them), then that is not necessary, will negatively (albeit temporarily) affect these URLs' page ranking, will obsolete bookmarks and links, and will slow down the user experience for those using those bookmarks and links.

It also requires that you change all links on your site to point to those new URLs if you want good results in search.

So, if the filepath needs to change, but the URL does not, then I'd suggest using an internal rewrite instead of an external redirect; Leave the URLs as-is, and simply 'map' them to the new filepath using the same code, but with internal rewrite syntax -- i.e. change the RewriteRule line to

 RewriteRule ^index.php$ /old/index.php [L] 

and leave your links alone.

Jim

Enigma5

5:50 pm on Oct 30, 2009 (gmt 0)

10+ Year Member



Thanks for the fast reply, that worked perfectly!

I would have used your 2nd suggestion except reviews920-1066 need to be directed to exact urls like this:

http://www.example.org/index.php?x=reviews&id=1065

needs to be:

http://www.example.org/new/seasonic-s12d-850w-power-supply-review/

[edited by: jdMorgan at 1:50 am (utc) on Oct. 31, 2009]
[edit reason] example.org [/edit]