Forum Moderators: phranque

Message Too Old, No Replies

301 Redirect without changing the url in the address bar

         

Abhihome

6:40 pm on Jan 8, 2010 (gmt 0)

10+ Year Member



Hi,

I am trying to do a 301 redirect and I would need to do that without changing the URL in the address bar.

Here is the rule I have:

RewriteCond %{HTTP_HOST} ^www\.staging\.example\.com$
RewriteCond %{REQUEST_URI} !^.. (This is to indicate that the rule should be executed if there is nothing after www.staging.example.com)
RewriteRule ^(.*) http://www.staging.example.com/example-sp.html [R=301,L]

Result:
Changing the URL to http://www.staging.example.com/example-sp.html when I invoke www.staging.abc.com.

Other Trials made on the third line:
1. RewriteRule ^(.*) http://www.staging.example.com/example-sp.html
Result: Same as above
2. RewriteRule ^(.*) /example-sp.html [R=301,L]
Result: Same as above
3. RewriteRule ^(.*) /example-sp.html
Result: 404 (This is because example-sp.html doesn't exist in document root. Any *-sp.html will pick *-sp.jsp from the app server)
4. No rule
Result: It picks the default index.html

Please help.
Thanks
Abhilash

[edited by: Abhihome at 7:10 pm (utc) on Jan. 8, 2010]

[edited by: jdMorgan at 2:29 am (utc) on Jan. 9, 2010]
[edit reason] example.com [/edit]

KenB

7:02 pm on Jan 8, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can either do a 301 redirect OR you can serve the other page without changing the URL in the address bar (assuming the domain doesn't change).

For instance you might do:

RewriteRule ^widgets/(ca¦in¦uk¦us)/$ /careers/index.html?cc=$1 [QSA,L]

jdMorgan

2:26 am on Jan 9, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Replace your whole rule with this.

RewriteCond %{HTTP_HOST} =www.staging.example.com
RewriteRule ^$ /abc-sp.html [PT,L]

Note that the "blank URL-path" test is now done in the rule, so the original RewriteCond is not needed.
The [PT] tells mod_rewrite to leave the output in "URL-format" instead of converting it to a filepath. Thus the rewritten URL-request can then be picked up by mod_proxy and sent to your back-end.

It that doesn't work, then use the [P] flag instead of [PT], and specify the *URL* of your back-end resource, e.g. "http://192.168.0.2:8080/abc-sp.jsp". The [P] flag will generate a reverse-proxy through-put, just as your likely-existing config-file mod_alias code does.

Note that the code above is for .htaccess or for use in a config file *within a <directory> container*. If used outside any <directory> container or .htaccess, add a leading slash to the RewriteRule pattern.

Jim

Abhihome

5:47 pm on Jan 9, 2010 (gmt 0)

10+ Year Member



Wow, Thanks Jim
That solved a real big problem.
One question; with the above Rewrite rule, which URL gets the SEO value, the source URL (www.staging.example.com) or the target URL (www.staging.example.com/abc-sp.html)?

Thanks Again!
Abhilash

g1smd

7:33 pm on Jan 9, 2010 (gmt 0)

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



The one in the link they clicked on the page, i.e. the one the user sees in the URL bar in their browser.

Abhihome

4:21 pm on Jan 11, 2010 (gmt 0)

10+ Year Member



[SOLVED]

Thanks
Abhilash