Forum Moderators: phranque

Message Too Old, No Replies

REGEX help for .htaccess redirect

         

viaterra

8:58 pm on Oct 20, 2010 (gmt 0)

10+ Year Member


Hello !

I was wondering if someone could help with a REGEX expression to set up a 301 redirect in my .htaccess.

I used to have iframes on my website, and people have linked to my pages using URLs such as http://domain.com?url=http://domain.com/page.htm

Now that I have improved the website's design and removed the iframe-based system, I would like to set up a 301 redirect to send all requests on :

http://domain.com?url=http://domain.com/whateverpage.htm

to :

http://domain.com/whateverpage.htm

I don't have experience with REGEX but I should think a simple and generic expression can take care of all such redirects.

Thanks in advance for any help.
Best regards,

Denis

phranque

12:24 pm on Oct 21, 2010 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



welcome to WebmasterWorld [webmasterworld.com], Denis!

you will need to use RewriteCond to Match/Extract a Query String:
http://www.webmasterworld.com/apache/3495477.htm [webmasterworld.com]

viaterra

1:07 pm on Oct 21, 2010 (gmt 0)

10+ Year Member



Thank for the answer, you've put me on track.
So it should be something like this :


RewriteCond %{QUERY_STRING} ^$
# check for empty query string to prevent looping

RewriteCond %{HTTP_REFERER} \?url=(.*)$
# capture the query string (after the ?url=)

RewriteRule .?url= %{REQUEST_URI}?url=%1 [R=301]
# redirect


Can you confirm if this is correct ?
Note that I have other URLs that contain ?language= or ?id= etc.. and those must not affected.

Thanks.

Denis

jdMorgan

2:14 pm on Oct 21, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> you will need to use RewriteCond to Match/Extract a Query String

# Redirect old frame-based URL requests to URL-path given in query string
# accept optional parm(s) before the "url=" name/value, but none after
RewriteCond %{QUERY_STRING} ^(([^&]*(&[^&]*)*)&)?url=http://(www\.)?domain\.com([^&]*)$ [OR]
# require parm(s) before and accepts optional parm(s) after the "url=" name/value
RewriteCond %{QUERY_STRING} ^(([^&]*(&[^&]*)*)&)url=http://(www\.)?domain\.com([^&]*)((&[^&]*)*)$
RewriteRule ^$ http://domain.com/%5?%2%6 [R=301,L]

This code can correctly retain additional query parameters preceding and/or following the "url=" name/value pair. It will also accept either "domain.com" or "www.domain.com" in the originally-requested query string.

That is, the rule will redirect
domain.com/?url=domain.com/<any-URL-path> to domain.com/<any-URL-path>/, or
domain.com/?<any-name1>=<any-value1>&url=domain.com/<any-URL-path> to domain.com/<any-URL-path>/?<any-name1>=<any-value1>, or
domain.com/?url=domain.com/<any-URL-path>&<any-name2>=<any-value2> to domain.com/<any-URL-path>/?<any-name2>=<any-value2>, or
domain.com/?<any-name1>=<any-value1>&url=domain.com/<any-URL-path>&<any-name2>=<any-value2> to domain.com/<any-URL-path>/?<any-name1>=<any-value1>&<any-name2>=<any-value2>
with "<any-name1>=<any-value1>" and "<any-name2>=<any-value2>" each representing one or more additional name/value pairs and either "domain.com" or "www.domain.com" specified in the requested query string.

Essentially, the "url=domain.com/<any-URL-path>" is "cleanly" removed from the query string, and the rest of the query string (if any) is retained.

See the resources cited in our Apache Forum Charter and the example threads and tutorials in our Apache Forum Library for more information. Links to both of these are at the top of this page.

The code snippet above assumes that you already have other working RewriteRules in your .htaccess file, and so have already enabled mod_rewrite.

Jim

viaterra

4:16 pm on Oct 23, 2010 (gmt 0)

10+ Year Member



Thanks for the answers and for the great help.
I'll see what I can do.