Blog
20
Sep
2008
1

Getting rid of the www with mod_rewrite and realURL

Written byBy: Mario Rimann.

Filed under TYPO3, Work .

The plan was to redirect all HTTP requests to www.domain.tld to domain.tld with a HTTP 301 (permanently redirected). Of course www.domain.tld/path/foo needed to be preserved and redirected to domain.tld/path/foo.

After struggling around for quite some time, I've found out why my rewrite rules were never really used: I had them below all the realURL-rewriting stuff within my .htaccess file. Moving the "get-rid-of-the-www" block to the top of the file solvedthe problem immediately .

I have the following snipped at the top of my .htaccess file:

# 301 redirect to domain without 'www.'
RewriteCond %{HTTP_HOST} ^www.domain.tld$ [NC]
RewriteRule ^(.*)$ domain.tld/$1 [R=301,L]

The "$1" in the last line is a back-reference to the first match of the RewriteRule (like a variable that's filled with the part after the domain name of the request). With this, the /path/foo is also written to the redirected URL.

Comments

Displaying results 1 to 1 out of 1
 
  • Gravatar: Dmitry Dulepov Dmitry Dulepov said,

    Sunday, 21-09-08 10:23

    You can do it easier. Use domain record and redirection inside it. No need for mod_rewrite at all :)

 

Leave a comment

Adding an entry to the guestbook
CAPTCHA image for SPAM prevention