Forum Moderators: phranque
I have the following structure in my ftp setup: / -- home -- mysitename -- www -- subdirectories.
OK, now I'm really confused. I am on a shared server. In directory / are all kinds of files I don't mess with like bash_history, bin, boot, etc. In directory "home" are everybody's sitenamed folders, including mine. In mysitename directory are more files I don't mess with, bash_history, .pinerc, .redirect (I have to look at that one), etc. plus my www folder. I now have .htaccess file in both the mysitename and www directories. Somebody please help me out here?
I am assuming that the www is what most of you are calling "root". Please correct me if I'm wrong.
Below is the code I'm using on a Unix server, which I believe is Apache.
-------------
Options +FollowSymLinks
AllowOverride FileInfo
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.com
RewriteRule (.*) [example.com...] [R=301,L]
------------
Put .htaccess in "mysite" directory seems to do the trick. So now I know which directory is my root. I ffeel like a dunce.
Any help, thanks in advance.
Your code looks good. The Options and AllowOverride are not always necessary, but if you are not crashing and the rewrites work you should be good to go.
I think you came from the G update thread... This is where I normally post, but haven't had too much time to spend here for the last week or two. (Sorry Jim).
The only change I would think about making is changing the condition to a negative, like this:
RewriteCond %{HTTP_HOST} !^www\.example\.com
There are a couple of reasons...
1. This helps break your site out of some types of frames (not all), because if the host making the request is not yours, the request for the page is re-written to your site.
2. The reason I really like it... If someone types in the wrong sub-domain, they will still end up at your site. EG wwww.yoursite.com (or even w4.yoursite.com) is a request for your domain with a wwww (or w4) sub-domain. When this request is processed if the requested sub-domain does not exist, the request is redirected to your site, and is then rewritten to the correct www version. (A request for an actual sub-domain will still be processed as normal, as long as it has a separate 'parent' or 'root' directory.)
Hope this helps.
Justin
I've managed to use
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
successfully to direct all http://example.com to http://www.example.com Headers check out as '301 moved permanently'.
I've put the code into htaccess in root. Other folders have their own htaccess files that parse static urls to various scripts / dynamic urls.
However, after setting up the 301 redirect, the header checker shows the permanent moved loction as the script and not the desired static url.
Is there a simple way that I can add something to htaccess in root to overcome the problem for all folders? Or is there something I need to do with each folder's htaccess file?
Thanks in advance!
What script? How was the static URL mapped to this script before you installed the domain redirect?
I suspect you may have a 301 redirect from the static URL to the 'script' -- when an internal rewrite should have been used. Please post more detail.
Jim
The folders are using
RewriteEngine on
Options +FollowSymLinks
RewriteCond %{REQUEST_FILENAME}!-f
RewriteRule ^([^/]+)$ $1/
RewriteCond %{REQUEST_FILENAME}!-d
RewriteRule ^([^/]+)/$ ../scriptfolder/scriptname.php?qry=$1 [L]
User goes to [domain.com...] and gets shown the content of scriptname.php with the filename posted as a variable qry= The .html gets stripped off in the php file.
[domain.com...] remains in the address bar. Header check shows 200 OK.
Previously [domain.com...] showed exactly the same (making me worry about duplicate content issues - mostly discussed in Google News Forum).
Have changed the htaccess in folder1 to
RewriteEngine on
Options +FollowSymLinks
RewriteCond %{HTTP_HOST} ^domain\.co\.uk
RewriteRule (.*) [domain.co.uk...] [R=301,L]
RewriteCond %{REQUEST_FILENAME}!-f
RewriteRule ^([^/]+)$ $1/
RewriteCond %{REQUEST_FILENAME}!-d
RewriteRule ^([^/]+)/$ ../scriptfolder/scriptname.php?qry=$1 [L]
This now includes a 301 redirect for [domain.co.uk...] to [domain.co.uk...] Headers show 301 Moved Permanently.
However, I had to add
RewriteCond %{HTTP_HOST} ^domain\.co\.uk
RewriteRule (.*) [domain.co.uk...] [R=301,L]
to a lot of folder htaccess files as a work around. I originally added it to root htaccess. I wondered if there was a directive to force the root htaccess 301 redirect on all folders (I assumed that this would be done automatically).
Although I've worked around it (successfully I think), it would be good to know for the future.
Hope this makes sense!. I'm a bit of a newbie to htaccess and mod rewrite. My files seem to work and show the correct headers but may not be ideal. Please feel free to put me right! What do you think?
Thanks again!
Here's another possibility: [webmasterworld.com...] msg#2.
It's also possible that some other server config problem exists that is interfering with your rules.
And be sure to flush your browser cache before testing *any* change to your code.
A comment:
RewriteCond %{REQUEST_FILENAME}!-f
RewriteRule ^([^/]+)$ $1/
RewriteCond %{REQUEST_FILENAME}!-d
RewriteRule ^([^/]+)/$ ../scriptfolder/scriptname.php?qry=$1 [L]
Your patterns are moderately exclusive, but if there is any way to make the rule patterns more specific, or to add additional exclusions by adding more RewriteConds above the -f/-d checks, then do so. A good way to bring a server down is to use ".*" as the pattern for -d/-f checks, forcing a check on *every* request. If you can make the rule pattern any more exclusive, then do so.
For example, you may wish to exclude image files, included JavaScript, and CSS files from being checked by adding
RewriteCond %{REQUEST_URI} !\.(jpe?g¦gif¦png¦js¦css)$ [NC] I should note that a similar problem exists with doing %{REMOTE_HOST} checks; These force the server to issue a reverse-DNS lookup request, with the client's request hanging until the reverse-DNS response is received. When using either file/directory exists checks or REMOTE_HOST lookups, it's important to limit them to cases where you know they are needed.
Jim
You stated above that you added:
RewriteCond %{HTTP_HOST} ^domain\.co\.uk
RewriteRule (.*) http://www.domain.co.uk[b]/folder1[/b]/$1 [R=301,L]
What happens if you handle them separately (which I'd recommend) and just fix the domain and let your other code add the subfolder after the redirect is performed (as it always has)?
RewriteCond %{HTTP_HOST} ^domain\.co\.uk
RewriteRule (.*) http://www.domain.co.uk[b]/$1[/b] [R=301,L]
1) How do you know if it's working? Is it that when you enter example.com into your browser it resolves as http://www.example.com?
2) If I put the line "Rewrite Engine on" in my .htaccess file, I get an internal server error. Why?
3) When I tried to discuss this with my webhost, what they did was place an index.html (I use php) file in my root directory which did an meta http-equiv="REFRESH" to my http://www.example.com/index.php homepage. Is this the same thing as a 301 redirect in .htaccess?
1) How do you know if it's working? Is it that when you enter example.com into your browser it resolves as http://www.example.com?
YES
2) If I put the line "Rewrite Engine on" in my .htaccess file, I get an internal server error. Why?
Your host probably does not have the module loaded or enabled.
3) When I tried to discuss this with my webhost, what they did was place an index.html (I use php) file in my root directory which did an meta http-equiv="REFRESH" to my http://www.example.com/index.php homepage. Is this the same thing as a 301 redirect in .htaccess?
Unfortunately, NO. This is a different type of redirect, which is browser side, and does not send the correct headers (from what you described) for a proper redirect to take place. A meta refresh is actually one of the techniques used to 'hijack' content.
A true redirect will send a header code of 302 (temporary move), or 301 (permanent move) which tells clients (browsers, and SE spiders) 'the page is not here any more, restart the request over here...'
My guess is you are on a 'free' hosting plan and do not have authorization to use mod_rewrite. Though the solution your host provided will work, it is *far* from ideal, and in some circumstances can be Search Engine death. I, personally, would be very careful about using this type of redirect, and would actually change hosts before I allowed this on my site.
With the current situation involving redirects and refreshes, I am extremely sceptical about using anything that does not fully inform a client (browser or SE) through the use of a proper HTTP header, of exactly what is happening and why.
I am sure Jim or someone else can elaborate on the differences between the two better than I can, so you might wait for a more thorough reply before making any final decision.
Hope this helps.
Justin
I have several links on search engines and such pointing to www.somesite.co.uk/~directory/alltypesofpages.htm
I own www.somesite.co.uk/ and have it parked on my
www.somesite.com/ webserver....with the content that those searh engines are looking for but they are
www.somesite.com/alltypesofpages.htm in that location.... so in the root of different domain name.
Is it possible to redirect the visitors to where the content is now using the methods described in this thread? If so could someone please make a suggestion...
www.somesite.co.uk/~directory/alltypesofpages.htm to
www.somesite.com/alltypesofpages.htm
so in effect....
any link that starts with
www.somesite.co.uk/~directory/
should point to
www.somesite.com/
Cheers...
Incoming Links to point from
www.example.com/~example/directory/file.html
to
www.example.com/directory/file.html [perm redirect]
Thanks.
We will be happy to help you write your own code, but we can't write code on demand here -- Doing so results in far too many requests for the few contributors to handle. Our forum charter [webmasterworld.com] explains this, and contains links to basic documentation for mod_rewrite and regular expressions to get you started.
Also, a search of the WebmasterWorld archives for existing redirection threads [google.com] may be helpful.
Note that you will need to place the code in either the www.example.com/ or www.example.com/~example/ directory in order for it to work.
Jim
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?example\.co\.uk
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
If someone visits
www.example.co.uk/anydirectory/anyfile.html
they will get redirected to
www.example.com/anydirectory/anyfile.html
with a 301 Perm Redirect In the header Information.
Now...that's great
Is this how I would also fix my other problem?
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?example\.co\.uk
RewriteCond %{HTTP_HOST} ^(www\.)?example\.co\.uk/~something
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
I'm not sure how your server(s) is/are set up, but just correcting what you have above would yield:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?example\.co\.uk
RewriteRule ^~something/(.*)$ http://www.example.com/$1 [R=301,L]
I am getting alot of 404s on my site because of old links to my other URLs.
I have 2 directories that used to be on my old site...and all the old links are pointing to them :(
I'm going to see if my tech support can help me out with this problem....
Thank You for all your help....
I may be back, you never know what tech support can do.
Q
1. You don't need to specify "RewriteEngine on" twice. Some module turn off when posed with a double statement, not sure about mod_rewrite.
2. An absolute must to read is: [httpd.apache.org...]
According to that document, the following should correct issues with a tilde in the request_uri:
RewriteRule ^/~something/(.+) [newserver...] [R,L]
I'm not great with mod_rewrite, and some of the advice above is extremely good, and probably more accurate, but this is something else to try.
That looked great. I gave it a try and nothing happened, but I think it's probaby of something else....:)
Here is what I have in my .htaccess file, including your suggested addition...i've added comments after each line to explain what I think each line does.
XBitHack on /* this is for my adsense include */
RewriteEngine on /* this turns on the RewriteEngine :) */
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.co\.uk /* if the URL being searched for matches www.domain.co.uk or domain.co.uk */
RewriteRule (.*) [domain.com...] [R=301,L] /* rewrite that URL to www.domain.com/whatever */
RewriteRule ^/~something/(.+) [domain.com...] [R=301,L] /* people visiting from .co.uk/~something/ or .com/~something/ will be redirected to www.domain.com/whatever */
RewriteRule ^/~somethingelse/(.+) [domain.com...] [R=301,L] /* people visiting from .co.uk/~somethingelse/ or .com/~somethingelse/ will be redirected to www.domain.com/whatever */
So I have 2 domains on 1 server.
The parked domain is the .co.uk
And I have links pointing to the .co.uk
The above code only redirects .co.uk links to .com
Should I be adding another RewriteCond?
I'm using the following to redirect my home page:-
RewriteEngine On
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST}!^www\.mysite\.com [NC]
RewriteRule (.*) http://www.mysite.com/$1 [R=301,L]
Should I modify the above somehow or include more RewriteCond/RewriteRules for the internal pages? What should that cond/rule look like? Should I just add "\path\" to the "\.com" in RewriteCond and "/path/" before the "$1" in the RerwriteRule?
[edited by: jdMorgan at 7:58 pm (utc) on July 11, 2005]
[edit reason] Unlinked [/edit]
OK. I tried this and added about 7 folders. I got an error from the server... can't remember the exact wording but it wouldn't let me access my home page because the were too many rules (or something).