Forum Moderators: phranque
I've been reading this forum for an age, so I thought it about time to join and pick your brains. I'm more of a coder than a webmin, so I hope you can help!
We're moving a site to another domain, changing the platform and I'm really struggling with redirects in a .htaccess file.
I have three problems:
1. If I set up a simple:
redirect 301 /categories/ [newsite.com...]
redirect 301 /categories/subcategories/category1.asp [newsite.com...]
All of the subpages are also redirected so for example if I go to: [oldsite.com...] I am forwarded to the newsite.com/categories/ but if I go to [oldsite.com...] I am forwarded [newsite.com...]
So is the 1st redirect interfering with the second? I really don't know what's going on here!
2. How do I redirect the root of the site to the new site without affecting everything else, so for example - I want to redirect [oldsite.com...] to [newsite.com...] and index.asp?
So if someone types in [oldsite.com...] they are taken to the newsite instead, I don't want a redirect all rule as the url and locations are now different, but the main root will still be the same as the root of the new site ;)
3. Lastly and this is really doing my head in, dynamic url's. We have hundreds of there.
Eg. [oldsite.com...] to [newsite.com...]
[mysite.com...]
[mysite.com...]
The biggest problem I have is number 1!
Thanks for all your help.
2) Do not redirect to example.com/index.asp, redirect to example.com/ and define index.asp as the DirectoryIndex page in order to map "/" to "index.asp" transparently. There is no reason to "publish" the redundant "index.asp" URL-path on the Web... Let me ask, when searching, do you go to google.com/ or to google.com/index.py?
3) If you want to drop the query strings, then append a "?" to the substitution URL in RedirectMatch. If you want to modify or "map" the query strings in some indirect fashion such as 74 --> unitedkingdom, then that's a whole new subject area, typically requiring a scripted solution, and best put off until clarified.
However, I suggest that you stop right here and re-consider: There is simply no need to change your URLs and tank your search rankings for weeks or months (up to nine, by some reports). You might consider simply mapping the old existing URLs to the new file architecture and technology transparently -- by using internal rewrites instead of external redirects. [Background [w3.org]]
Understand that "file types" appended to URLs are meaningless on the Web, and only take on meaning after the server translates a URL to a filepath and invokes a content handler. But the content handler that is invoked is under your control, and there is no reason that a URL ending in .asp cannot be processed by the PHP interpreter. URLs ending in .asp can invoke PHP content-handling just as easily as they can invoke ASP content handling. In a URL, the "filetype" on the end is completely arbitrary... it is just "text," and has no meaning to browsers or search robots. What does have meaning is the Content-Type header returned by the server in response to URL requests (the Content-Type header contains the MIME-type of the content that the server is sending back to the client, e.g. "text/html" or "image/gif").
If you do decide to actually change your URLs, then I would suggest going "extensionless" so that your new URLs are in the form "example.com/associations?country=74" or "example.com/associations/unitedkingdom". There is no need to publish the "file type" on the Web, and getting rid of it now means that you will never have to go through this exercise again, say, should you switch your server technology to Cold Fusion or .NET in the future. Filetypes are simply not needed for "page" URLs, and really only have utility when "included objects" are being referred to -- things like .gif images, for example, or .css stylesheets.
Forum note: Although it's tempting to try to post all of your questions at once, you may get better responses from a wider group of people --each with different areas of expertise-- by breaking up your questions into more tightly-focused posts. Take things one-at-a-time, in other words.
Jim
The 'most-complex pattern to least-complex' you mentioned above works really well, by moving the top level redirects (ie, the category directory) down in the .htaccess file, it no longer affects all the content below (because it's at the bottom), so that's the 1st problem solved.
As for 2, the new site is php, but you are right and I would just redirect it to /newsite.com/, but what's the actual code to do this please, and again, bottom of the file?
Number 3, whoa, that's a lot to take in, I'm just about to start reading through the Apache documentation I think ;)
Thanks so much for your help JD, a master indeed.
3) If you want to drop the query strings, then append a "?" to the substitution URL in RedirectMatch.
RewriteCond %{REQUEST_METHOD} ^GET$
RewriteCond %{QUERY_STRING} !^$
RewriteRule .* %{REQUEST_URI}? [R=301,L]
1. Is the "?" in "%{REQUEST_URI}?" dropping the query strings? Don't they have to be dropped by default since there is no [QSA] flag out there?
2. Is there any difference between
RewriteCond %{QUERY_STRING} !^$
and
RewriteCond %{QUERY_STRING} .
I mean is there something like "no query string" and "empty query string" in mod_rewrite?
0) Yes, valid for mod_rewrite.
1) Yes, dropping the query string. No, they are not dropped if there is no "?" in the substitution URL. If there is no "?" in the substitution URL, the query string is passed through unchanged. If there is a "?" then the query string is replaced if [QSA] is not specified, or appended if [QSA] is specified.
The literal meaning of "?" is "replace query string unless [QSA], else append". If there is no query string after the "?", then it means "replace query string with blank unless [QSA], else append."
2) The patterns "!^$" and "." simply mean "NOT blank" and "contains something" respectively, and thus are simply two complementary ways of saying the same thing. However, "." is shorter and does not require a logical inversion operation, and so is faster to process.
(I have permitted this slightly-off-topic diversion due to the final two sentences below. If a continuation is desired, please open a new thread and refer back to this one. Thanks.)
darrencornwell,
Please post your best effort at coding it for yourself, as we're glad to help, but this is a discussion forum and not really a "support desk" or "free coding service." Our purpose, as outlined by our Forum Charter (see link at top of page), is to help you learn. As such, specific questions are welcome.
Jim