Forum Moderators: phranque

Message Too Old, No Replies

mod rewrite keep url for centralized app

mod_rewrite keep url for centralized app

         

oewete

12:38 am on Oct 24, 2008 (gmt 0)

10+ Year Member



Hi, i'm a newbie, i've built a centralized application eg. http://www.quux-foo.com such that the look & feel is determined by the Server variable [REFERRER_URL] to determine which logo & CSS style to customize http://www.quux-foo.com looks .e.g. if i go to http://www.example.com/quux-foo/ it should redirect to http://www.quux-foo.com while keeping http://www.example.com/def/ in the status/url bar

My issues are :
1) .htaccess in http://www.example.com/quux-foo/

Options +FollowSymLinks
RewriteEngine on
#
# Prevent .htaccess recursion if index.php is requested
RewriteCond $1 !^index\.php$
# else do the rewrite
RewriteRule ^(.*)$ "http\:\/\/www\.quux-foo\.com\/" [L]

This redirects to http://www.quux-foo.com without keeping http://www.example.com/quux-foo/ in the status bar

2)The Referrer Url server variable is not detected.

I would appreciate if there's a solution to my 2 issues, most especially the 1st issue.

[edited by: jdMorgan at 2:42 am (utc) on Oct. 24, 2008]
[edit reason] non-resolving domains example.com & quux-foo [/edit]

g1smd

2:02 am on Oct 24, 2008 (gmt 0)

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



To keep the same URL, you need a rewrite, rather than the redirect in your example..

Having a domain name and R=301 in the target, forces the redirect. Omit both to form a rewrite.

However, this system sounds like the same content is going to appear at multiple URLs. That could be a big problem.

jdMorgan

2:32 am on Oct 24, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You may need to use a RewriteCond to check the %{HTTP_REFERER} variable (taken from the HTTP request header) and suppress the rewrite or do something else if it is blank. Clients are not required to send a referrer, many 'internet security suites' suppress them, and caching proxies don't use them, because these proxies fetch pages on behalf of potentially hundreds of users who sit behind those proxies and are served the cached pages from that proxy -- Your server only sees one request from the proxy itself, and it may then serve its cached copy to many users. Therefore the referrer is meaningless in that context and is not sent, because each of those users may reach your page from a different referrer. Corporations and ISPs like AOL and Earthlink use such caching proxies, and their users have no choice in the matter.

All-in-all, the HTTP Referer should not be used for anything that is important and has to work 100% of the time. If your plans depend on this, you're going to have to find a better way such as including the referrer in the link itself (as a query string parameter), or using a different URL-path (or subdomain) per referrer.

As g1smd says, you'll also need to make sure that search engines are always redirected to one and only one version of the site at one and only one URL. Otherwise, you have to deal with the risks of duplicate content: You will not get to choose which URL they show, or which version of the site they index. This code needs to be based on a frequently-maintained list of search engine IP addresses -- It must be 'bulletproof' and you may have to pay an outside service to be sure your IP list is up-to-the-minute fresh. Also, do not promote multiple domains or multiple versions of the site -- only one.

Jim

jdMorgan

3:25 am on Oct 24, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Also, in case you use the rule you posted as a basis for modifications, it would be better written as:

# Prevent .htaccess recursion if index.php is requested
RewriteCond $1 !^index\.php$
# else do the rewrite
RewriteRule (.*) http://www.quux-foo.com/ [R=302,L]

Only characters within regular-expressions patterns need to be escaped with "\" and fewer of them need to be escaped that in other "languages" such as PERL or PHP.

If a redirect is implied by the use of a canonical substitution URL, it is best practice to specify whether a 300, 301, 302, 303, or 307 redirect is desired.

Also, to add to the general discussion, if example.com and quux-foo.com are not hosted in the same server filespace, then you may need to reverse proxy requests from example.com to the quux-foo application server. See Apache mod_proxy for details. There are several complications, in that you may need to configure example.com to send the X-Forwarded-For HTTP header containing the original requestor's IP address, and set up custom logging (see Apache mod_log_config) on quux-foo.com so that X-Forwarded-For is logged instead of the (default) Remote-Addr. Otherwise, the logs on quux-foo will show that *all* requests come from example.com, and you will not be able to get meaningful application server logfile analysis using that data.

Jim

oewete

7:35 am on Oct 24, 2008 (gmt 0)

10+ Year Member



Thanks for your responses jdMorgan, however all i need is to maintain the current url http://www.example.com/quux-foo/ in address bar while it rewrites internally to [quux-foo.com...] & also assume that they are on the same server & have the same IP address.

i rewrote the .htaccess to :

Options +FollowSymLinks
RewriteEngine on
#
# Prevent .htaccess recursion if index.php is requested
RewriteCond $1 !^index\.php$
# else do the rewrite
RewriteRule (.*) [quux-foo.com...] [R=302,L]

&

Options +FollowSymLinks
RewriteEngine on
#
# Prevent .htaccess recursion if index.php is requested
RewriteCond $1 !^index\.php$
# else do the rewrite
RewriteRule (.*) [quux-foo.com...]

both didnt maintain the http://www.example.com/quux-foo/ in the address bar rather it changed to [quux-foo.com...]

g1smd

9:15 am on Oct 24, 2008 (gmt 0)

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



Including a domain name in the target makes it a redirect. A redirect forces a new HTTP transaction for a new URL. The default is a 302 redirect, unless you state that it should be 301 or something else. So, yes, the code you show above is a redirect, not a rewrite.

However, a 302 redirect typically says to search engines "Keep the URL you first saw, and index the content under that. Pretend you haven't seen where the redirect pointed to."

oewete

11:16 am on Oct 24, 2008 (gmt 0)

10+ Year Member



Ok then, since both domains are on the same server & the file path for http://www.example.com/quux-foo/ is /home/example/public_html/quux-foo/

is it possible to rewrite to /home/quux-foo/public_html/index.php

i've tried it though but it showed me a 404 error, on checking the logs

[Thu Oct 23 13:07:56 2008] [error] [client 41.222.70.210] File does not exist: /home/example/public_html/quux-foo/home/quux-foo/public_html/index.php

g1smd

11:32 am on Oct 24, 2008 (gmt 0)

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



You don't need the full server filepath, just the part that "could" form a URL if it were to be exposed to the outside world.

jdMorgan

1:02 pm on Oct 24, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, just rewrite to "/"

RewriteCond <conditions>
RewriteRule <your-patter[b]n> / [L][/b]

Jim

oewete

1:32 pm on Oct 24, 2008 (gmt 0)

10+ Year Member



HI Jim, can u give me an example, i dont seem to get your last post.

jdMorgan

1:33 pm on Oct 24, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You did not post your new rule, so I gave the only example that I could.

Jim

oewete

3:13 pm on Oct 24, 2008 (gmt 0)

10+ Year Member



Options +FollowSymLinks
RewriteEngine on
#
# Prevent .htaccess recursion if index.php is requested
RewriteCond $1 !^index\.php$
# else do the rewrite
RewriteRule ^/(.*)$ /home/quux-foo/public_html/index.php$1 [L]

jdMorgan

3:39 pm on Oct 24, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Remove the DocumentRoot path, since the server will always add that on its own. Based on your previous posts, I think what you want is:

Options +FollowSymLinks
RewriteEngine on
#
# Rewrite all requests for /quux-foo/<anything> to "/<anything>"
RewriteRule [b]^/quux-foo/(.*)$ /$1[/b] [L]

Jim

g1smd

3:40 pm on Oct 24, 2008 (gmt 0)

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



If that
(.*)
rule is in the root of the site, all requests for robots.txt, stylesheet files, bot ID files ( for webmastertools etc) and images will also be sucked into that rule.

The server location only needs to specify the parts that could be addressed from the outside world (i.e. omit the

/home/quux-foo/public_html
part).

[jd posted at the same time, with corrected code]