Forum Moderators: phranque

Message Too Old, No Replies

mod rewrite: Remove www

         

Fireball22

8:09 pm on Feb 9, 2009 (gmt 0)

10+ Year Member



Hello,

I've got the following mod_rewrite to redirect to different "virtual subfolders" depending on your language:


RewriteEngine on

RewriteRule ^(de¦en)$ http://www.%{HTTP_HOST}/$1/ [R=301,L]
RewriteRule ^(de¦en)/(index\.php)?$ index.php?language=$1 [QSA,L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(de¦en)/(.*)$ $2?language=$1 [QSA,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(de¦en)/(.*)$ $2 [L]

RewriteCond %{HTTP_HOST} !^www\.[^.:]+\.(co\.[a-z]{2}¦[a-z]{2,6})\.?(:[0-9]+)?$
RewriteRule (.*) http://www.%{HTTP_HOST}/$1 [R=301,L]

Now I want to redirect directly to the Domain itself and not anymore to the www.DOMAIN.

So I tried to change the code as follows:


RewriteEngine on

#Remove the "www." in the first RewriteRule
#Externally redirect; Add "www"
RewriteRule ^(de¦en)$ http://%{HTTP_HOST}/$1/ [R=301,L]
RewriteRule ^(de¦en)/(index\.php)?$ index.php?language=$1 [QSA,L]

# Rewrite all URL-paths in en/ and de/ subdirectories
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(de¦en)/(.*)$ $2?language=$1 [QSA,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(de¦en)/(.*)$ $2 [L]

#What should I do here?
# Externally redirect to add "www" and remove trailing periods
# and/or port numbers on requested non-www hostnames
RewriteCond %{HTTP_HOST} !^www\.[^.:]+\.(co\.[a-z]{2}¦[a-z]{2,6})\.?(:[0-9]+)?$
RewriteRule (.*) http://www.%{HTTP_HOST}/$1 [R=301,L]

I am looking forward to your ideas!

Michael

Fireball22

11:27 am on Feb 10, 2009 (gmt 0)

10+ Year Member



I got an e-mail notification because of a new post, but unfortunately I can't see anything?
Is this a bug?

Michael

jdMorgan

3:15 pm on Feb 10, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Looks like a bug. Please see the "report a problem" link at the bottom of every WebmasterWorld page.

Your rule changes do not reflect your stated goal. Working from the code in your original post, you'd need:


RewriteEngine on
#
# Externally redirect requests for non-canonical "www" subdomain to non-www domain
RewriteCond %{HTTP_HOST} ^www\.([^.:]+\.(co\.[a-z]{2}¦[a-z]{2,6}))\.?(:[0-9]+)?$
RewriteRule (.*) http://%1/$1/ [R=301,L]
#
# Internally rewrite URL-path requests for /<language>/ or /<language>/index.php
# to script filepath /index.php?language=<language>
RewriteRule ^(de¦en)/(index\.php)?$ index.php?language=$1 [QSA,L]
#
# If requested URL-path /<language>/<anything> does NOT resolve to an existing directory,
# internally rewrite those requests to the filepath /<anything>?language=<language>
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(de¦en)/(.*)$ $2?language=$1 [QSA,L]
#
# If requested URL-path /<language>/<anything> does NOT resolve to an existing
# file, internally rewrite those requests to the filepath /<anything>
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(de¦en)/(.*)$ $2 [L]

I should note that the logic of your last two rules does not seem correct. The rules will work as I described in the comments, but it is unlikely that the last rule will ever execute. The last rule will only be invoked if the requested URL-path *exists* as a directory, but does NOT exist as a file, and if that requested URL-path does not match the conditions required to invoke the first of the three internal rewrite rules.

Jim

vetzkov

3:41 pm on Feb 10, 2009 (gmt 0)

10+ Year Member



It wont work with the way it is written right now. Someone maybe will fix it?

Fireball22

8:03 pm on Feb 10, 2009 (gmt 0)

10+ Year Member



@jdMorgan
Thank you very much for your professional help!
You are exactly right, I will try it as soon as possible!

@vetzkov
Did you replace the broken pipes?

Michael

Fireball22

11:27 pm on Feb 14, 2009 (gmt 0)

10+ Year Member



I am sorry, but I wasn't able to test the new script!

I had uploaded the file via ftp an visited my homepage to check the script.
Unfortunately I wasn't able to visit more than one page of my website, because the Apache needed all the system memory at once for this request.
I removed the htaccess from my server, but I couldn't visit any site of my website.
The only resoluten was to restart Apache, and then it worked.

I don't know what is wrong, but I know, that this is a bug off course.
But I don't know if the script is wrong or my apache ;)

Michael

[edited by: Fireball22 at 11:29 pm (utc) on Feb. 14, 2009]