homepage Welcome to WebmasterWorld Guest from 54.161.240.10
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
.htaccess redirect without query string
Redirecting without query string using htaccess
ashish2005




msg:4587425
 12:47 pm on Jun 25, 2013 (gmt 0)

I have already asked this question at one more place but I did not get any response. I am sure I will get help with my .htacess file here like I did in the past.

What I am trying to do using .htacess is to redirect old urls to new urls . Now 301 redirects are the best way to do this according to my understandings. I googled around and used the following code to redirect

.htacess file:

RedirectMatch 301 /play-([0-9a-zA-Z?-]+)-online-([0-9]+).html http://www.example.com/online-games/$1.html

I am actually trying to redirect http://www.example.com/play-Fighting-Mario-online-541.html to http://www.example.com/online-games/fighting-mario.html

What actually happens after the redirect is that I see the following url in the title bar:

http://www.example.com/online-games/Fighting-Mario.html?task=view&id=1110&name=Haunted-House

Everything is ok, except the query strings after the ? part. I only need till .html .

I have tried everything, searching around, using similar codes of others using Redirectmatch. I am really poor at .htacess and find it really difficult (Regex and all). I tried combinations of codes but to no avail.

I would be really grateful if someone here helped me out.

Thank You

 

phranque




msg:4587468
 4:13 pm on Jun 25, 2013 (gmt 0)

you want to use a mod_rewrite directive (RewriteRule) instead of a mod_alias directive.
end your substitution string with a question mark to avoid passing through the query string.

ashish2005




msg:4587490
 5:36 pm on Jun 25, 2013 (gmt 0)

Are you telling me to change that RewriteMatch in my code above with RewriteRule?

lucy24




msg:4587503
 6:12 pm on Jun 25, 2013 (gmt 0)

Now 301 redirects are the best way to do this according to my understandings.

This is correct, but you need to distinguish between "what to do" and "how to do it". A 301 redirect is the desired end result. You can get there in a variety of ways including:

Redirect(Match) 301 etcetera (mod_alias in htaccess or config file)
RewriteRule blahblah [R=301,L] (mod_rewrite in htaccess or config file)
php page that sends out a 301 response header combined with new URL

If the redirect involves a query string, you can't use mod_alias (Redirect or RedirectMatch by that name). This, in turn, means that if you have any existing rules using mod_alias, those rules also need to be translated to mod_rewrite form.

If you search this Forum you will find lots of posts about how to change or remove the query from a redirect target. But before you go there, we need to know where the query is coming from. mod_alias didn't simply invent it; it had to be there already.

phranque




msg:4587517
 7:01 pm on Jun 25, 2013 (gmt 0)

Are you telling me to change that RewriteMatch in my code above with RewriteRule?


there's more to it than just changing the name of the directive, but if you are doing anything with the query string besides a pass-through, then yes you need the RewriteRule Directive:
http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewriterule

ashish2005




msg:4587806
 12:27 pm on Jun 26, 2013 (gmt 0)

quote:lucy24
...we need to know where the query is coming from. mod_alias didn't simply invent it; it had to be there already.


The query is coming from another RewriteRule in .htaccess which is

RewriteRule ^play-([0-9a-zA-Z?-]+)-online-([0-9]+) index.php?task=view&id=$2&name=$1 [L]

quote:phranque
RewriteRule blahblah [R=301,L]


The [R=301,L] seems to do nothing with my urls.

I tried

RewriteRule 301 /play-([0-9a-zA-Z?-]+)-online-([0-9]+).html http://www.example.com/online-games/$1.html [L,R=301]

That does not redirect the url to anything. It just opens the url the way it is.

phranque




msg:4587831
 1:49 pm on Jun 26, 2013 (gmt 0)

there's a 301 that doesn't belong in that RewriteRule (check the documentation i linked to for syntax) and you should probably add start and end anchors to the Pattern.

make sure your external redirects are before your internal rewrite or you will expose your internal urls.

g1smd




msg:4587832
 1:50 pm on Jun 26, 2013 (gmt 0)

The leading slash should probably be omitted from your pattern. Replace with ^ perhaps.

Escape the literal period in the pattern.

Are you sure you need a question mark in the character group?

lucy24




msg:4587945
 7:59 pm on Jun 26, 2013 (gmt 0)

The query is coming from another RewriteRule

You say "another" RewriteRule but in fact your originally quoted redirect is made with mod_alias (Redirect by that name). You MUST change everything to mod_rewrite form, or nobody can be answerable for the consequences ;)

ashish2005




msg:4589553
 11:30 am on Jul 2, 2013 (gmt 0)

This is just too complicated for me. I have just given up. I just decided to let the query strings stay.

Thanks Guys

phranque




msg:4589672
 7:58 pm on Jul 2, 2013 (gmt 0)

don't quit so easily or you have wasted my time, lucy24's time and g1smd's time.

301 /play-([0-9a-zA-Z?-]+)-online-([0-9]+).html


1. get rid of the 301 and leading slash
2. replace it with a ^ which is a start anchor
3. get rid of the ? in the character group
4. escape the literal period with a backslash
5. add a $ to the end of the pattern which is an end anchor
6. add a ? after the substitution string so the query string doesn't pass through to the redirected url.
7. done!

if you have any other mod_rewrite or mod_alias directives such as RewriteRule or Redirect/RedirectMatch then you should probably show us.

ashish2005




msg:4590498
 11:03 am on Jul 5, 2013 (gmt 0)


don't quit so easily or you have wasted my time, lucy24's time and g1smd's time.


Thank you for saying that. I definitely do not want to have wasted time of anyone. So I am giving this another go.


1. get rid of the 301 and leading slash
2. replace it with a ^ which is a start anchor
3. get rid of the ? in the character group
4. escape the literal period with a backslash
5. add a $ to the end of the pattern which is an end anchor
6. add a ? after the substitution string so the query string doesn't pass through to the redirected url.
7. done!


I tried what you said and if I am correct after modification, this is what becomes of my code

RewriteRule ^play-([0-9a-zA-Z-]+)-online-([0-9]+)\.html$ http://www.example.com/online-games/$1.html? [L,R=301]

And now to my delight it works perfectly just like I wanted it to. Almost gave up. Thanks for everything.

lucy24




msg:4590636
 8:49 pm on Jul 5, 2013 (gmt 0)

:: big round of applause for phranque ::

phranque




msg:4590676
 11:32 pm on Jul 5, 2013 (gmt 0)

cheers!
=8)

g1smd




msg:4590794
 11:49 am on Jul 6, 2013 (gmt 0)

Since you're not reusing $2, the rule might even simplify to
RewriteRule ^play-([0-9a-zA-Z-]+)-online http://www.example.com/online-games/$1.html? [R=301,L]
as long as the new pattern doesn't match requests that it should not.

Can there be another hyphen between the play- and -online parts in the old style URLs? If not, omit "-" from the character group.

lucy24




msg:4590795
 12:23 pm on Jul 6, 2013 (gmt 0)

Can there be another hyphen between the play- and -online parts in the old style URLs?


First post:
... redirect http://www.example.com/play-Fighting-Mario-online-541.html to ...

:(

g1smd




msg:4590855
 5:01 pm on Jul 6, 2013 (gmt 0)

Didn't read that bit. :)

Does the new URL need to have the .html extension?
This could be the right time to go extensionless.

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