Welcome to WebmasterWorld Guest from 54.145.209.34

Forum Moderators: Ocean10000 & incrediBILL & phranque

http://xyz.com/ to http://www.xyz.com/

That cool .htacess trick !

   
6:39 am on Apr 3, 2005 (gmt 0)

10+ Year Member



I want all users to refer my Web site from a single url i.e., http://www.example.com/

Please write that 3-4 lines of .htaccess trick here. Thank you very much.

[edited by: jdMorgan at 3:16 pm (utc) on April 3, 2005]
[edit reason] Examplified. [/edit]

6:44 am on Apr 3, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



RewriteEngine On
RewriteCond %{HTTP_HOST}!^www\.example.com\.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

replace 1

[edited by: jdMorgan at 3:16 pm (utc) on April 3, 2005]
[edit reason] Examplified. [/edit]

7:00 am on Apr 3, 2005 (gmt 0)

10+ Year Member



done; replaced 1 with my domain name and uploaded the file into public_html directory.
It is not working as the url 'google.co.in' in the browser 'Address' bar :

[edited by: jdMorgan at 3:14 pm (utc) on April 3, 2005]
[edit reason] Removed specifics per TOS. [/edit]

7:03 am on Apr 3, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It is not working as the url 'google.co.in' in the browser 'Address' bar :

what u mean?

7:20 am on Apr 3, 2005 (gmt 0)

10+ Year Member



It is not working as I asked ;)
Please type both urls in browser Address bar and see/compare it yourself ( leave out http:// and www )
May be our code is wrong.

Regards

8:42 am on Apr 3, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



hi

the code i gave you will make that all the users which enter your website by name without www will go to with www.website.com

place this in your htaccess file

i still can't get what you want to do with google?

8:53 am on Apr 3, 2005 (gmt 0)

WebmasterWorld Senior Member topr8 is a WebmasterWorld Top Contributor of All Time 10+ Year Member



i think anshul wants to actually redirect

example.com

to

www.example.com

(rather than do an internal rewrite)

8:59 am on Apr 3, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



but his post heading is telling something else
10:54 am on Apr 3, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



And besides, the mod_rewrite code given DOES do an external 301 redirect. I can only imagine that anshul scrambled something in it before using.

PS: Personally I create seperate virtual servers, but perhaps I shouldn't ;)

SN

11:26 am on Apr 3, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Make sure there is a space between the curly bracket and the exclamation mark:

.. HOST} !^www ..
3:30 pm on Apr 3, 2005 (gmt 0)

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Permanently redirect example.com to www.example.com:

RewriteCond %{HTTP_HOST} ^example\.com
RewriteRule (.*) http://www.example.com [R=301,L]

Permanently redirect www.example.com to example.com:


RewriteCond %{HTTP_HOST} ^www\.example\.com
RewriteRule (.*) http://example.com [R=301,L]

Permanently redirect anything except www.example.com to www.example.com:


RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteRule (.*) http://www.example.com [R=301,L]

This only works for HTTP/1.1 or above. HTTP/1.0 clients will not provide a Host header, so {HTTP_HOST} will be blank. The code therefore detects this situation and disables itself to prevent an infinite redirection loop.

Permanently redirect anything except example.com to example.com:


RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^example\.com
RewriteRule (.*) http://example.com [R=301,L]

This only works for HTTP/1.1 or above. HTTP/1.0 clients will not provide a Host header, so {HTTP_HOST} will be blank. The code therefore detects this situation and disables itself to prevent an infinite redirection loop.

If you use any of the samples above and are redirected to Google, it means you have a browser hijacker on your computer, or that someone has modified the "hosts" file on that computer. On Win XP, the hosts file is in C:\WINDOWS\SYSTEM32\DRIVERS\etc. It is a plain-text file and can be edited with NotePad. If this is a personal machine, then it is almost always safe to delete the hosts file (or just rename it to hosts.old).

Jim

6:53 am on Apr 5, 2005 (gmt 0)

10+ Year Member



Great!
msg #:11 is the pure code :)
8:48 pm on Apr 5, 2005 (gmt 0)



The code I hacked together seems slightly different from that in #11. I want to redirect everything under www.example.com to example.com:

www.example.com/adir/afile.html

should redirect to:

example.com/adir/afile.html

etc.

Note the caret in the last line of the rewrite code below: ^(.*) Should that be there, or should I remove it?

I also have a few other .htaccess files that have things like this in them:

RedirectPermanent /adir/index.html [a_different_domain.com...]

Will the "L" in the last line of the rewrite code have an effect on these other .htaccess files?

Should I consolidate all of my .htaccess files into one .htaccess file in the root, placing the RedirectPermanent's before the rewrite code? Or, does it not matter, and the "L" only applies to that one .htaccess file?

I tried the rewrite code out briefly and it seemed to work and the other redirects worked too, but I want to be sure before I leave it there for any length of time.

- BEGIN REWRITE CODE -
RewriteEngine on
Options +FollowSymlinks
RewriteBase /
RewriteCond %{HTTP_HOST} www.example.com
RewriteRule ^(.*)$ [example.com...] [R=301,L]
- END REWRITE CODE -

9:24 pm on Apr 5, 2005 (gmt 0)

10+ Year Member



And where exacly this code should be implemented?

In httpd.conf or .htaccess file?

Where, if required for only one of several virtual hosts?

Regards

1:01 am on Apr 6, 2005 (gmt 0)

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member



When used with a single instance of (.*) or .* in a pattern, the caret and dollar sign anchors are superfluous. This is because "*" is "greedy" and will match as many characters as possible -- whether or not the pattern is anchored.

If you have privileges to put the code in httpd.conf, it's much more efficient there. This is because code in httpd.conf is compiled at server restart, whereas code in .htaccess is interpreted for each HTTP request. But you do have to restart your server before any httpd.conf changes take effect.

If you want the rules to apply to only one virtual host, then put them in that VirtualHost container.

Note the subtle differences between URL patterns in .htaccess and httpd.conf; the URL is localized to the current directory in .htaccess, but not in httpd.conf. As a simple example, to detect a request for index.html in .htaccess, you'd use a RewriteRule pattern of "^index\.html$" whereas in httpd.conf, it would be "^/index\.html$".

Don't confuse mod_rewrite directives with mod_alias directives. mod_rewrite flags like [L] won't work for mod_alias.

The order in which you place directives for different modules makes no difference in Apache; Each module takes its turn scanning your files for directives it understands and executes them. This order is determined by the LoadModule list order (reversed) in Apache 1.x, and by pre-defined module "priority" in Apache 2.x.

Jim

1:16 pm on Apr 6, 2005 (gmt 0)

10+ Year Member



What it all meant ( flags )?
[NC], [L], [R], [OR], [P], [C], [R=301,L], [R=301], [QSA,L]

I suppose, it'll take a minute or two, for you experts to answer it here.

I fear mod_rewrite :¦
1:21 pm on Apr 6, 2005 (gmt 0)

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Please see the Apache mod_rewrite documentation, RewriteRule Flags section for more info. There is a link to this document in our forum charter [webmasterworld.com].

Jim

 

Featured Threads

My Threads

Hot Threads This Week

Hot Threads This Month