Welcome to WebmasterWorld Guest from 54.226.27.104

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

.htaccess redirect without query string

Redirecting without query string using htaccess

     

ashish2005

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

5+ Year Member



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

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

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



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

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

5+ Year Member



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

lucy24

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

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



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

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

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



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

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

5+ Year Member



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

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

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



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

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

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



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

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

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



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

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

5+ Year Member



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

Thanks Guys

phranque

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

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



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

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

5+ Year Member




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

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

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



:: big round of applause for phranque ::

phranque

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

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



cheers!
=8)

g1smd

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

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



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

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

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



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

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

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



Didn't read that bit. :)

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

Featured Threads

Hot Threads This Week

Hot Threads This Month