Forum Moderators: phranque
I have just started to explore the world of .htaccess as a method of permanently redirecting traffic. After much searching I managed to cobble together a couple of files that appear to work.
However, due to my ignorance, I dont really understand why and although they work am I using the methods correctly?
I have two requirements:
1. Permanantly redirect a single .html file to an external URL
2. Permantly redirect a domain name to another whilst maintaining the original directory structure on the new domain.
Both of these requirements are unrelated to each other.
These are the "solutions" being implemented currently:
For requirement 1:
RewriteEngine On
RewriteRule index.html [someone-elses-domain.com...] [R=301,QSA,L]
For requirement 2:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?soon-to-be-my-old-domain.com [NC]
RewriteRule ^(.*)$ [newdomain.com...] [L,R=301]
So, my questions are:
a) Should "requirement 1" be written like "requirement 2" (or visa versa)?
b) Is QSA necessary?
c) Does it matter that "L" is before or after "R=301"?
d) How can subdomains be handled in one file (example: requirement 2)?
e) Bearing in mind that in "requirement 2" the soon-to-be-my-old-domain.com will be expiring soon, is there a better / different way of permanently redirecting in case it upsets spiders?
f) Should I start over / give up?!
I appreciate that this may seem very much ".htaccess - Day 1" but I am looking to start off with a scalable solution and any help in laymans terms and complete examples would be much appreciated.
Many thanks in advance.
Not bad for day one... in fact your are very close to being there.
1. Should requirement 1 be like 2, etc.
You could actually restructure like this:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^index.html$ http: //www.someone-elses-domain.com/ [R=301,L]
RewriteCond %{HTTP_HOST} ^(www\.)?soon-to-be-my-old-domain.com [NC]
RewriteRule . http: //www.newdomain.com/dir1 [R=301,L]
As long as you have a line space in the middle you should be fine.
Please, notice I have made a couple of minor adjustments:
1. I added a true beginning ^ and ending $ to your first rule.
2. I removed QSA, which stands for Query String Append... This tells your server to pass the information as a query srting (stuff after the? EG index.php?page=stuff&page2=more-stuff) and allows for more information after the? of a rewrite. (Normally used to pass multiple conditions.) In answer to your question, no in your case it is not necessary.
3. I believe you should always put the L last, to make sure you have proper execution of all directives/flags. (Don't quote me on this one.)
4. For efficiency, and since you are not passing the information to the new URL, I removed the variable creation () and the 'catch all' .* The new version simply checks to see if there is any character, then moves on to the Condition if there is. If you need to redirect to a specific page you could change your rule to:
RewriteRule ^(.*) http: //www.newdomain.com/$1 [R=301,L]
Example only where $1=the full path request after www.your-current-site.com
5. d) How can subdomains be handled in one file (example: requirement 2)?
Not sure exactly what you are asking here.
6. Why would you quit/give up when you are right there?
7. Remember to remove the spaces in http: // (someday I'll figure out the right codes for this board)
Hope this helps.
Justin
Added:
It looks like you would like to keep your directory structure intact... so you would need to use the rule in my example (4), or something similar.
To cover the points you mentioned, yes I am maintaining the directory structure as is. I have copied over all the files to the new server and now, for example, if you go to www.soon-to-be-my-old-domain.com/dir1/index.html it redirects to www.newdomain.com/dir1/index.html
Everything lines up nicely.
And reference the point about subdomains (your answer #5), I have been trying to write one RewriteCond to point www.soon-to-be-my-old-domain.com, yyy.soon-to-be-my-old-domain.com, zzz.soon-to-be-my-old-domain.com (as examples) to just www.newdomain.com. Currently, each subdomain has its own separate .htaccess file
i.e.
RewriteCond %{HTTP_HOST} ^(www\.)?soon-to-be-my-old-domain.com [NC] in root folder
RewriteCond %{HTTP_HOST} ^(yyy\.)?soon-to-be-my-old-domain.com [NC] in the yyy folder
RewriteCond %{HTTP_HOST} ^(zzz\.)?soon-to-be-my-old-domain.com [NC] in the zzz folder
I am guessing that there is a wildcard solution but Day 1 has been a long day and my head hurts! :-)
Many thanks again for replying and for the confidence building help
Regards
GD
Your solution to the subdomain question should be fine. It is often better to avoid unneccessary 'catch-alls' or 'wildcards'. If you only have one or two rules, no point in making anything be more complicated than it needs to be.
What you are thinking should be fine.
You can use the same type of regex in a condition as you would in a rule. EG (.*) or (.+), etc.
Justin