Forum Moderators: phranque

Message Too Old, No Replies

Apache redirection

         

Jake1234

1:56 pm on Apr 13, 2012 (gmt 0)

10+ Year Member



I am using rewrite rule engine to redirect from one domain to another and it works fine. Here's the code:

RewriteCond %{REQUEST_URI} !^/mobile
RewriteCond %{HTTP_USER_AGENT} "BlackBerry|Opera|Palm|Symbian|Android|iPhone|iPod" [NC]
RewriteCond %{REQUEST_URI} ^/homepage/web/homepageRedirectAction\.action
RewriteRule .? [%{HTTP_HOST}%1...] [R=301,L]

Once I am in the /mobile domain for certain links I would like to redirect back to /homepage. So I have this code e.g.

redirect 301 /mobile [%{HTTP_HOST}%1...]

However, it is entering into infinite loop and on firefox browser I receive the message "The website isn't redirecting properly"

So I would like to know how can I redirect back to the original domain?

Thanks for your help

wilderness

6:47 pm on Apr 13, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Don't mix mod_rewrite and mod_alias (redirect 301).

Try your 2nd redirect in mod_rewrite, or, make some exclusions in your original lines that omit the later second attempt.

Jake1234

6:51 pm on Apr 13, 2012 (gmt 0)

10+ Year Member



I did try following:

RewriteCond %{REQUEST_URI} ^/mobile
RewriteCond %{HTTP_USER_AGENT} "iPad" [NC]
RewriteCond %{REQUEST_URI} !^/homepage
RewriteRule .? [%{HTTP_HOST}%1...] [R=301,L]

However, it still did not work

wilderness

7:02 pm on Apr 13, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Here's a near identical inquiry to yours [webmasterworld.com]

FWIW, a site search on "https://%{HTTP_HOST}%" brought that up.

Jake1234

8:48 pm on Apr 13, 2012 (gmt 0)

10+ Year Member



Thanks much. I got it working.

I have one problem to solve.

I want to redirect
/blogs/3aee4c5c-38cc-4f99-b484-c24f00b6624c/

to

/mobile/blogs/BlogEntries?handle=3aee4c5c-38cc-4f99-b484-c24f00b6624c

where the number 3aee4c5c-38cc-4f99-b484-c24f00b6624c is dynamic. How can I create a dynamic condition for it? Do I have to use RewriteMap?

wilderness

11:49 pm on Apr 13, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



site search on "string" or "query"

g1smd

1:58 am on Apr 14, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



If the hyphens are always in the same place you can use a simple pattern to do this.

RewriteRule ^([0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12})$ /mobile/blogs/BlogEntries?handle=$1 [l]

lucy24

3:09 am on Apr 14, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Possibly even

/blogs/([\h-]+)/

if you want to pick up everything. (Does mod_rewrite do \h? I forget.) Unless you've got ordinary files named, uh, /blogs/bead/ or /blogs/faded/ or similar.

g1smd

8:31 am on Apr 14, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I mean to say

RewriteRule ^blogs/([0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12})$ /mobile/blogs/BlogEntries?handle=$1 [L] 

Jake1234

3:40 am on Apr 15, 2012 (gmt 0)

10+ Year Member



I am trying something like this:

RewriteCond %{HTTP_USER_AGENT} "iPad" [NC]
RewriteCond %{REQUEST_URI} ^/blogs/([0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12})/$
RewriteCond %{QUERY_STRING} ^lang=en_us
RewriteRule .? [%{HTTP_HOST}%1...] [R=301,L]

It is redirecting but handle's value is blank (http://www.myhost.com/mobile/blogs/BlogEntries?handle=). How can I pass the value "([0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12})" to handle query parameter?

Thanks much for your help

lucy24

5:20 am on Apr 15, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Do a search for Query + mod_rewrite and the boilerplate will pop right up :)

g1smd

5:39 pm on Apr 15, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Use example.com in this forum.

Make the condition you want to capture from, the last condition of the set.

Jake1234

3:44 pm on Apr 16, 2012 (gmt 0)

10+ Year Member



Thanks everyone for providing me leads.

Finally this is how I got it working. Please share your ideas if there's scope for improvement in terms of performance, and other factors.

RewriteCond %{REQUEST_URI} ^/blogs/([0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12})
RewriteCond %{QUERY_STRING} ^lang=en_us
RewriteCond %{REQUEST_URI} ^/blogs/+(.*)$
RewriteRule .? [%{HTTP_HOST}...] [R=301,L]

lucy24

4:01 pm on Apr 16, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Urk. You don't want HTTP_HOST. Part of any redirect's job is to regularize your domain name: with or without www, strip any extraneous ports, that kind of thing.

Double Urk:
RewriteCond %{REQUEST_URI} ^/blogs/+(.*)$

Do you get a lot of requests containing multiple slashes in this location? Well, maybe you do, but maybe you meant something else.

Most of your Conditions can go in the Rule itself, making everything run faster and smoother. So

RewriteCond %{REQUEST_URI} ^/blogs/([0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12})
RewriteCond %{QUERY_STRING} ^lang=en_us
RewriteCond %{REQUEST_URI} ^/blogs/+(.*)$
RewriteRule .? https://%{HTTP_HOST}/mobile/blogs/BlogEntries?handle=%1 [R=301,L]


reduces to

RewriteCond %{QUERY_STRING} ^lang=en_us
RewriteRule ^blogs/+([0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12}) https://www.example.com/mobile/blogs/BlogEntries?handle=$1 [R=301,L]


If you want to keep the existing query string, add a QSA flag. By default, a new query-- including a final blank question mark-- overwrites any existing one. And note that your anchor means that the rule will only work if "lang=en_us" is the very first thing in the query string.

But wait... I missed something earlier. Why are you redirecting from a (relatively) short pretty URL to a longer one with a query string? Wouldn't you rather be rewriting?

Jake1234

5:53 pm on Apr 18, 2012 (gmt 0)

10+ Year Member



@lucy24, Thanks for your suggestions.

We actually have 2 different applications - one for mobile devices and other for web. The reason I am redirecting to a longer URL.

The other problem I am running into and probably there's no solution for it is the anchor tag (#).

I have a URL [example.com...]

which I want to redirect to

[example.com...]

where "W2eaf940769da_4dc2_a93d_c1da88053110" is dynamically generated.

I can't find any way that I could process anything after # since anything after # is ignored by the server (that is I can't get to process /wiki/W2eaf940769da_4dc2_a93d_c1da88053110)

Though I know it may not be possible to achieve that but by any chance does anyone know of any hook?

Any help is much appreciated.

g1smd

3:08 am on Apr 20, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



since anything after # is ignored by the server

Correction. Anything after # isn't requested by the browser.

lucy24

6:06 am on Apr 20, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Anything after # isn't requested by the browser.

Do you mean that if you follow a link like

http://www.example.com/coolpage.html#greatstuff

the browser tucks away the "greatstuff" part in its own memory for later* use, and only asks example.com's server for "coolpage.html"? So the server technically doesn't "ignore" the fragment link, because it never knew about it in the first place?


* For a given definition of "later" :)

Jake1234

3:46 pm on Apr 20, 2012 (gmt 0)

10+ Year Member



I believe you got that right.

My understanding is anything following # is not sent to the server from the client.

g1smd

9:56 pm on Apr 20, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Examine the GET request that the server receives.

There's no # in it.