homepage Welcome to WebmasterWorld Guest from 54.211.180.175
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
301 redirect URLs containing special characters
301 redirect URLs containing special characters
george2013




msg:4581008
 11:26 am on Jun 4, 2013 (gmt 0)

We recently created a new website with another provider. The old website has been deleted and a .htaccess 301 redirect created. 90% of them work fine, but noticed that that for the following redirect I get '500 Internal Server Error'.

Redirect 301 /category.php?id=12&cat=s http://example.com/

The issue probably is the ? and the & symbol, but I am not sure how and what to add to the rewrite code.

Any suggestions are welcome.

Regards,
George

 

lucy24




msg:4581119
 7:50 pm on Jun 4, 2013 (gmt 0)

mod_alias (Redirect by that name) only works on paths. If you need to redirect a specific query string you'll need to use mod_rewrite (RewriteRule with flag [R=301,L]).

Ordinarily this would mean you have to shift everything else to mod_rewrite as well-- but since it's a sitewide redirect you can probably fake it by just making sure the rule is in the appropriate place.

But a more general question: This specific URL is getting redirected to the root. Suppressing side discussion about advisability or otherwise of doing this in any case. I have to assume that at the end of all your page-specific redirects you have a global mop-up in the form (don't quote me, I'm improvising)

RedirectMatch 301 /.* http://example.com/

where you're not actually capturing anything, just using RedirectMatch to override the ordinary reappend-the-path behavior of mod_alias. So anything that hasn't already been redirected gets sent along to the front page of the new site.

Why don't you simply let /category.php?id=12&cat=s get picked up by this final redirect? Do requests for /category.php with some other parameter get sent somewhere else?

george2013




msg:4581211
 8:42 am on Jun 5, 2013 (gmt 0)

Hi Lucy,

Thank you for your post.

The
RedirectMatch 301 /.* http://example.com/
almost did the trick. I should have copied all the rules in the first place, here they are:

Options +FollowSymLinks

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^/]+)/?$ /product.php?id=$1 [QSA,L]
ErrorDocument 404 error404.html

RedirectMatch 301 /dir/(.*) http://example.com/dir/old/$1


I haven't used htaccess in the past, so apologies if any if this is confusing.

The
RedirectMatch 301 /.* http://example.com/
worked, but it also redirected following URL - which work fine without this line -
Redirect 301 /PRODUCT http://example.com/accessories.html


Thanks,
G

lucy24




msg:4581221
 9:15 am on Jun 5, 2013 (gmt 0)

Any mopping-up line should come at the very end of all rules. This part is true of any redirects anywhere: put the specific rules before the general ones. Within any given module, rules will execute in the order you put them.

So for example if you wanted to take
/category.php?id=12&cat=s
and send it to its own place:

RewriteCond %{QUERY_STRING} (^|&)id=12(&|$)
RewriteCond %{QUERY_STRING} (^|&)cat=s(&|$)
RewriteRule ^category\.php http://www.example.com/new-wonderful-special-page.html? [R=301,L]

(the trailing ? is to make the query string go away, assuming that's what you want)
and then everything with a different query falls down to

RewriteRule ^category\.php http://www.example.com/not-so-special-page-for-the-others [R=301,L]

and then everything for all other pages ends up at

RewriteRule .* http://www.example.com/leftover-page-for-the-bottom-of-the-heap.html

With mod_alias:

Redirect301 /product http://example.com/accessories.html

et cetera for any specific pages or directories that need individual handling, winding up with

RedirectMatch 301 /.* http://www.example.com/bottom-of-the-barrel.html


Rules beginning in
RedirectMatch /.*
should pick up requests for absolutely everything on the domain.

But if you really are mixing mod_alias (Redirect by that name) and mod_rewrite, be careful about the order. mod_rewrite will almost certainly execute before mod_alias, regardless of which one is physically first in htaccess. So put the RewriteRules first as a reminder to yourself that all of them will happen first.

Have only one mopping-up rule. If you're combining mods, the mopup will have to go on the mod_alias side. If you express it as a RewriteRule, anything using mod_alias (Redirect or RedirectMatch) will never get a chance to run.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved