PDA

View Full Version : Redirects with subdomains



equusmedical
07-19-2004, 06:48 AM
Hi all,

Hopefully this is quite useful to someone who is trying to work with redirects and subdomains.

I wanted people to be redirected to a shopping cart on my main domain:

the 1st domain is:

www.equusmedical.com

and I wanted it redirected to:

http://www.equusmedical.com/cgi-bin/tlsstore.cgi?user_action=link&link=mainpage

This was easy and originally setup a .htaccess file with a redirect so that any entry to equusmedical.com went to the other URL.

This was done by putting the following code into .htaccess:


redirect /index.html http://www.equusmedical.com/cgi-bin/tlsstore.cgi?user_action=link&link=mainpage

My problem came about when I got a subdomain: www.podiatryreview.com

When I tried to access my subdomain root directory I was automatically forwarded by the .htaccess code to to equusmedical shopping cart URL.

From a bit of reading on the web I found that I needed to use the rewrite engine command in the .htaccess file to detect which URL was being written and then redirect, or technically in this case, rewrite the URL.

The syntax for this is:


RewriteEngine on

RewriteCond %{HTTP_HOST} ^(www\.)?equusmedical\.com$ [NC]
RewriteRule ^(.*)$ http://www.equusmedical.com/cgi-bin/tlsstore.cgi?user_action=link&link=mainpage [R=301,L]


if you have more than multiple subdomains you can check the URL on more than one occasion and refer on to each e.g.:


RewriteEngine on

RewriteCond %{HTTP_HOST} ^(www\.)?equusmedical\.com$ [NC]
RewriteRule ^(.*)$ http://www.equusmedical.com/cgi-bin/tlsstore.cgi?user_action=link&link=mainpage [R=301,L]

RewriteCond %{HTTP_HOST} ^(www\.)?podiatryreview\.com$ [NC]
RewriteRule ^(.*)$ http://www.podiatryreview.com/somewhere else$1 [R=301,L]

Most people should be able to follow that syntax to work out what they need to do.

Let me know if you can think of any other simple ways of doing the same thing.

Hope this helps someone, took me a couple of hours of searching to come up with the answer. The stuff written on .htaccess, redirects and particularly rewrite engine is not written for amateurs like me.

Regards,

Matt

equusmedical
07-19-2004, 07:15 AM
Okay as a follow-up.

Just realised there are limitations to this as well!

Sorry!

Noticed, that when I returned to my website I couldn't access the control panel manager.

When I entered the URL:

www.equusmedical.com/manager

I was redirected to the shopping cart.

Not such a perfect result after all.

If anyone has an answer to this please let us all know.

Thanks,

Matt

jalal
07-19-2004, 08:37 AM
Well, you could:

a) enter a RewriteRule to match on the manager URL and then have it take no action (use the [L] flag).
b) use a negative RewriteCond for the manager URL, sort of like:

RewriteCond %${HTTP_HOST} !^(www\.)?equusmedical\.com/manager.*$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?equusmedical\.com$ [NC]
RewriteRule ^(.*)$ etc.etc.etc

This says if the url is not 'www.equusmedical.com/manager' but is 'www.equusmedical.com' then redirect it.
I'm going by memory on the above, so you'll have to check I got it right...

HTH

equusmedical
07-20-2004, 09:23 PM
Thanks for this but I think this would be too awkward because there would be too many exceptions for the rewrite rule. Every script you run in or under the cgi-bin directory would be effected, as well as of course other subfolders.

It seems we need a rewrite rule that only applies to the Root directory. I am suprised this is such an obvious limitation to the syntax of the rewrite command.

Any other thoughts?

Matt

jalal
07-21-2004, 12:48 AM
It seems we need a rewrite rule that only applies to the Root directory.

You can do that as well.

You have complete flexibility with mod_rewrite to do what you want.

equusmedical
07-25-2004, 07:50 AM
Being the amateur that I am I continued to slave onwards trying to find the perfect syntax for what I required. I think I have finally found the answer.

My entire .htaccess rewrite syntax, is this:


RewriteEngine on
Options +FollowSymlinks
RewriteBase /
RewriteRule ^\.htaccess$ - [F]
RewriteRule ^\.*$ http://www.equusmedical.com/cgi-bin/tlsstore.cgi?user_action=link&link=mainpage [R]
RewriteCond %{HTTP_USER_AGENT} ^EmailSiphon [OR]
RewriteCond %{HTTP_USER_AGENT} ^ExtractorPro

This includes security to prevent people finding my .htaccess file as well as offering a couple of conditions to prevent some spam email grabbing.

The important part for redirection is this:



RewriteRule ^\.*$ http://www.equusmedical.com/cgi-bin/tlsstore.cgi?user_action=link&link=mainpage [R]

This is obviously just for my store but gives you the general idea.

This allows me to have people redirected from my base address to my webstore. However, it doesn't have the problem of redirecting from my subdomain back to my original domain and webstore.

Hope that helps someone out there who has spent a few hours looking at the Apache server manual.

Matt

jalal
07-26-2004, 12:40 AM
Hi Matt

mod_rewrite is quite a world of its own... just a couple of comments on what you have above.

.htaccess files are already protected, if you look in the /etc/httpd/conf/httpd.conf file and search for the lines starting with '<Files ~ "^\.ht">' that is the mechanism that Apache uses to protect files from viewing.

Once you have the rules setup to your satisfaction, it is a good idea to move them into /etc/httpd/conf/httpd.conf for efficiency reasons. When the rules exist in the .htaccess file then they are read by Apache for every page visited in that directory. When they are specified in the httpd.conf file then they are just read once when Apache is started. This does mean that you need to restart Apache after changing the rules.

wildjokerdesign
03-06-2005, 08:08 AM
Allthough this is an old thread I wanted to get clarification on something. Jalal you mentioned that the .htaccess file is already protected via the httpd.conf. Is it not the same for the Options +FollowSymlinks?

Then once you have this working via .htaccess and you want to move it to your httpd.conf where is the best place for it? Would you simply past what you had in the .htaccess file into the httpd.conf or are there parts you do not need like the RewriteEngine on directive?

More then likely this is pretty basic stuff but it still eludes me and I am trying to better understand. Last question is... I have tried to find a good refrence for knowing what the code at the end of each line i.e. the stuff in []. Some I can reason out [F] = Forbidden or Fail? Is there a good listing of these somewhere?

jalal
03-06-2005, 11:49 AM
Allthough this is an old thread I wanted to get clarification on something. Jalal you mentioned that the .htaccess file is already protected via the httpd.conf. Is it not the same for the Options +FollowSymlinks?

'Options' can appear almost anywhere in httpd.conf or in .htaccess files. If 'Options +FollowSymlinks' appears in the global section of the file, then it applies to the whole server (unless modified elsewhere), and if it appears inside a Directory section it applies to that directory and so on.


Then once you have this working via .htaccess and you want to move it to your httpd.conf where is the best place for it? Would you simply past what you had in the .htaccess file into the httpd.conf or are there parts you do not need like the RewriteEngine on directive?

You would paste the code so that it affected the area that you needed affecting. Sorry if that sounds a little vague, but it really depends.
If you have something in .htaccess that you only want to affect that sub-domain, then you would put it in the Virtual Hosts section for that domain for example. If you were blocking access to a directory, then you would put it in a Directory setting.
You would use RewriteEngine On only once in an httpd file, unless you wanted to turn it off for a section and then back on. Or you can just turn it on for a particular VirtualHost


More then likely this is pretty basic stuff but it still eludes me and I am trying to better understand. Last question is... I have tried to find a good refrence for knowing what the code at the end of each line i.e. the stuff in []. Some I can reason out [F] = Forbidden or Fail? Is there a good listing of these somewhere?

That's easy:
http://httpd.apache.org/docs/mod/mod_rewrite.html
(look for RewriteRule, about 3/4 of the way down).

Hope all that helps.
8)

wildjokerdesign
03-06-2005, 03:17 PM
Thank you it does!