homepage Welcome to WebmasterWorld Guest from 54.226.180.223
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
http://xyz.com/ to http://www.xyz.com/
That cool .htacess trick !
anshul




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

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]

 

benevolent001




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

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]

anshul




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

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]

benevolent001




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

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

what u mean?

anshul




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

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

benevolent001




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

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?

topr8




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

i think anshul wants to actually redirect

example.com

to

www.example.com

(rather than do an internal rewrite)

benevolent001




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

but his post heading is telling something else

killroy




msg:1509816
 10:54 am on Apr 3, 2005 (gmt 0)

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

Span




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

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

.. HOST} !^www ..

jdMorgan




msg:1509818
 3:30 pm on Apr 3, 2005 (gmt 0)

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

anshul




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

Great!
msg #:11 is the pure code :)

blogger




msg:1509820
 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 -

bg011ca




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

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

jdMorgan




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

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

anshul




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

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 :¦

jdMorgan




msg:1509824
 1:21 pm on Apr 6, 2005 (gmt 0)

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

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved