Forum Moderators: phranque
I am pleased to be new here in this community.
If got the following problem with my mod_rewrite:
RewriteEngine on
RewriteRule ^((de¦en))/(.*)? $2/?language=$1 [QSA,L]
If I type in:
www.example.com/en/ it should resolve it to www.example.com/index.php?language=en
The same should happen if I type in www.example.com/en/index.php
Unfortunately I get Error404 if I type in the first solution.
The second solution causes an Error500.
Here you can see the rewrite-log:
First solution:
127.0.0.1 - - [21/Dec/2008:12:56:34 +0100] [localhost/sid#6b5148][rid#fc8f70/initial] (3) [perdir D:/Programme/wamp/www/] strip per-dir prefix: D:/Programme/wamp/www/en -> en
127.0.0.1 - - [21/Dec/2008:12:56:34 +0100] [localhost/sid#6b5148][rid#fc8f70/initial] (3) [perdir D:/Programme/wamp/www/] applying pattern '^((de¦en))/(.*)?' to uri 'en'
127.0.0.1 - - [21/Dec/2008:12:56:34 +0100] [localhost/sid#6b5148][rid#fc8f70/initial] (1) [perdir D:/Programme/wamp/www/] pass through D:/Programme/wamp/www/en
127.0.0.1 - - [21/Dec/2008:12:57:35 +0100] [localhost/sid#6b5148][rid#f43da8/initial] (3) [perdir D:/Programme/wamp/www/] add path info postfix: D:/Programme/wamp/www/en -> D:/Programme/wamp/www/en/index.php
127.0.0.1 - - [21/Dec/2008:12:57:35 +0100] [localhost/sid#6b5148][rid#f43da8/initial] (3) [perdir D:/Programme/wamp/www/] strip per-dir prefix: D:/Programme/wamp/www/en/index.php -> en/index.php
127.0.0.1 - - [21/Dec/2008:12:57:35 +0100] [localhost/sid#6b5148][rid#f43da8/initial] (3) [perdir D:/Programme/wamp/www/] applying pattern '^((de¦en))/(.*)?' to uri 'en/index.php'
127.0.0.1 - - [21/Dec/2008:12:57:35 +0100] [localhost/sid#6b5148][rid#f43da8/initial] (2) [perdir D:/Programme/wamp/www/] rewrite 'en/index.php' -> 'en/?language=en'
127.0.0.1 - - [21/Dec/2008:12:57:35 +0100] [localhost/sid#6b5148][rid#f43da8/initial] (3) split uri=en/?language=en -> uri=en/, args=language=en
127.0.0.1 - - [21/Dec/2008:12:57:35 +0100] [localhost/sid#6b5148][rid#f43da8/initial] (3) [perdir D:/Programme/wamp/www/] add per-dir prefix: en/ -> D:/Programme/wamp/www/en/
127.0.0.1 - - [21/Dec/2008:12:57:35 +0100] [localhost/sid#6b5148][rid#f43da8/initial] (2) [perdir D:/Programme/wamp/www/] strip document_root prefix: D:/Programme/wamp/www/en/ -> /en/
127.0.0.1 - - [21/Dec/2008:12:57:35 +0100] [localhost/sid#6b5148][rid#f43da8/initial] (1) [perdir D:/Programme/wamp/www/] internal redirect with /en/ [INTERNAL REDIRECT]
127.0.0.1 - - [21/Dec/2008:12:57:35 +0100] [localhost/sid#6b5148][rid#f43078/initial/redir#1] (3) [perdir D:/Programme/wamp/www/] add path info postfix: D:/Programme/wamp/www/en -> D:/Programme/wamp/www/en/
127.0.0.1 - - [21/Dec/2008:12:57:35 +0100] [localhost/sid#6b5148][rid#f43078/initial/redir#1] (3) [perdir D:/Programme/wamp/www/] strip per-dir prefix: D:/Programme/wamp/www/en/ -> en/
127.0.0.1 - - [21/Dec/2008:12:57:35 +0100] [localhost/sid#6b5148][rid#f43078/initial/redir#1] (3) [perdir D:/Programme/wamp/www/] applying pattern '^((de¦en))/(.*)?' to uri 'en/'
127.0.0.1 - - [21/Dec/2008:12:57:35 +0100] [localhost/sid#6b5148][rid#f43078/initial/redir#1] (2) [perdir D:/Programme/wamp/www/] rewrite 'en/' -> 'en/?language=en'
127.0.0.1 - - [21/Dec/2008:12:57:35 +0100] [localhost/sid#6b5148][rid#f43078/initial/redir#1] (3) split uri=en/?language=en -> uri=en/, args=language=en&language=en
127.0.0.1 - - [21/Dec/2008:12:57:35 +0100] [localhost/sid#6b5148][rid#f43078/initial/redir#1] (3) [perdir D:/Programme/wamp/www/] add per-dir prefix: en/ -> D:/Programme/wamp/www/en/
127.0.0.1 - - [21/Dec/2008:12:57:35 +0100] [localhost/sid#6b5148][rid#f43078/initial/redir#1] (2) [perdir D:/Programme/wamp/www/] strip document_root prefix: D:/Programme/wamp/www/en/ -> /en/
127.0.0.1 - - [21/Dec/2008:12:57:35 +0100] [localhost/sid#6b5148][rid#f43078/initial/redir#1] (1) [perdir D:/Programme/wamp/www/] internal redirect with /en/ [INTERNAL REDIRECT]
127.0.0.1 - - [21/Dec/2008:12:57:35 +0100] [localhost/sid#6b5148][rid#f3e9a0/initial/redir#2] (3) [perdir D:/Programme/wamp/www/] add path info postfix: D:/Programme/wamp/www/en -> D:/Programme/wamp/www/en/
127.0.0.1 - - [21/Dec/2008:12:57:35 +0100] [localhost/sid#6b5148][rid#f3e9a0/initial/redir#2] (3) [perdir D:/Programme/wamp/www/] strip per-dir prefix: D:/Programme/wamp/www/en/ -> en/
127.0.0.1 - - [21/Dec/2008:12:57:35 +0100] [localhost/sid#6b5148][rid#f3e9a0/initial/redir#2] (3) [perdir D:/Programme/wamp/www/] applying pattern '^((de¦en))/(.*)?' to uri 'en/'
127.0.0.1 - - [21/Dec/2008:12:57:35 +0100] [localhost/sid#6b5148][rid#f3e9a0/initial/redir#2] (2) [perdir D:/Programme/wamp/www/] rewrite 'en/' -> 'en/?language=en'
127.0.0.1 - - [21/Dec/2008:12:57:35 +0100] [localhost/sid#6b5148][rid#f3e9a0/initial/redir#2] (3) split uri=en/?language=en -> uri=en/, args=language=en&language=en&language=en
127.0.0.1 - - [21/Dec/2008:12:57:35 +0100] [localhost/sid#6b5148][rid#f3e9a0/initial/redir#2] (3) [perdir D:/Programme/wamp/www/] add per-dir prefix: en/ -> D:/Programme/wamp/www/en/
127.0.0.1 - - [21/Dec/2008:12:57:35 +0100] [localhost/sid#6b5148][rid#f3e9a0/initial/redir#2] (2) [perdir D:/Programme/wamp/www/] strip document_root prefix: D:/Programme/wamp/www/en/ -> /en/
127.0.0.1 - - [21/Dec/2008:12:57:35 +0100] [localhost/sid#6b5148][rid#f3e9a0/initial/redir#2] (1) [perdir D:/Programme/wamp/www/] internal redirect with /en/ [INTERNAL REDIRECT]
127.0.0.1 - - [21/Dec/2008:12:57:35 +0100] [localhost/sid#6b5148][rid#28f76a0/initial/redir#3] (3) [perdir D:/Programme/wamp/www/] add path info postfix: D:/Programme/wamp/www/en -> D:/Programme/wamp/www/en/
127.0.0.1 - - [21/Dec/2008:12:57:35 +0100] [localhost/sid#6b5148][rid#28f76a0/initial/redir#3] (3) [perdir D:/Programme/wamp/www/] strip per-dir prefix: D:/Programme/wamp/www/en/ -> en/
127.0.0.1 - - [21/Dec/2008:12:57:35 +0100] [localhost/sid#6b5148][rid#28f76a0/initial/redir#3] (3) [perdir D:/Programme/wamp/www/] applying pattern '^((de¦en))/(.*)?' to uri 'en/'
127.0.0.1 - - [21/Dec/2008:12:57:35 +0100] [localhost/sid#6b5148][rid#28f76a0/initial/redir#3] (2) [perdir D:/Programme/wamp/www/] rewrite 'en/' -> 'en/?language=en'
127.0.0.1 - - [21/Dec/2008:12:57:35 +0100] [localhost/sid#6b5148][rid#28f76a0/initial/redir#3] (3) split uri=en/?language=en -> uri=en/, args=language=en&language=en&language=en&language=en
127.0.0.1 - - [21/Dec/2008:12:57:35 +0100] [localhost/sid#6b5148][rid#28f76a0/initial/redir#3] (3) [perdir D:/Programme/wamp/www/] add per-dir prefix: en/ -> D:/Programme/wamp/www/en/
127.0.0.1 - - [21/Dec/2008:12:57:35 +0100] [localhost/sid#6b5148][rid#28f76a0/initial/redir#3] (2) [perdir D:/Programme/wamp/www/] strip document_root prefix: D:/Programme/wamp/www/en/ -> /en/
127.0.0.1 - - [21/Dec/2008:12:57:35 +0100] [localhost/sid#6b5148][rid#28f76a0/initial/redir#3] (1) [perdir D:/Programme/wamp/www/] internal redirect with /en/ [INTERNAL REDIRECT]
127.0.0.1 - - [21/Dec/2008:12:57:35 +0100] [localhost/sid#6b5148][rid#fc0808/initial/redir#4] (3) [perdir D:/Programme/wamp/www/] add path info postfix: D:/Programme/wamp/www/en -> D:/Programme/wamp/www/en/
127.0.0.1 - - [21/Dec/2008:12:57:35 +0100] [localhost/sid#6b5148][rid#fc0808/initial/redir#4] (3) [perdir D:/Programme/wamp/www/] strip per-dir prefix: D:/Programme/wamp/www/en/ -> en/
127.0.0.1 - - [21/Dec/2008:12:57:35 +0100] [localhost/sid#6b5148][rid#fc0808/initial/redir#4] (3) [perdir D:/Programme/wamp/www/] applying pattern '^((de¦en))/(.*)?' to uri 'en/'
127.0.0.1 - - [21/Dec/2008:12:57:35 +0100] [localhost/sid#6b5148][rid#fc0808/initial/redir#4] (2) [perdir D:/Programme/wamp/www/] rewrite 'en/' -> 'en/?language=en'
(...)
I am looking forward to your ideas!
Sorry for my bad English and best wishes from Germany!
Michael
RewriteRule ^(de¦en)$ /$1/ [R=301,L]
RewriteRule ^(de¦en)/(.*) $2?language=$1 [QSA,L] 1st problem: Your rule did not handle /en (w/o trailing slash). We redirect those requests to /en/
2nd problem: There's a second parenthesis where none should be.
BTW: Change your DirectoryIndex to index.php accordingly.
[edited by: Caterham at 3:15 pm (utc) on Dec. 21, 2008]
Sure, if index.php is in the request, the rewrite happens - but that rewrite causes Duplicate Content, as this is an additional URL serving the same content.
You should set up a redirect (301) to strip off any index filename (and fix www for that request at the same time) before invoking the rewrite.
Additionally, you should redirect any non-www URLs to www to fix that Duplicate issue before the rewrite happens too.
The main concern with $2 is that when someone requests /en/thatfile.html the rewrite will target /thatfile.html/index.php?language=en instead. Is that correct?
Thank you very much for your help!
Now I doesn't get an Error500 anymore, but unfortunately I get still Error404 for "Not Found" if I type in for example www.example.com/en/ or www.example.com/en/index.php.
@g1smd
Yes that's right, if you request /en/thatfile.html then the target should be /thatfile.html/index.php?language=en.
In the index.php I will check, if GET was set an which content it contains (f.e. "en").
After that this script redirects to the URL www.example.com/en/.
Michael
RewriteRule ^(de¦en)$ /$1/ [R=301,L]
RewriteRule ^(de¦en)/(.*) $2/index.php?language=$1 [QSA,L] Rewriting to index.php directly instead of the folder will get rid of one subrequest and one internal fast redirect.
I tried the following case and it works fine, but I think it's only coincidence that it will be resolved true.
If I type in www.example.com/en/hello.php instead of www.example.com/en.
Log:
127.0.0.1 - - [23/Dec/2008:01:20:36 +0100] [localhost/sid#6b5148][rid#fbe538/initial] (3) [perdir D:/Programme/wamp/www/] add path info postfix: D:/Programme/wamp/www/de -> D:/Programme/wamp/www/de/hello.php
127.0.0.1 - - [23/Dec/2008:01:20:36 +0100] [localhost/sid#6b5148][rid#fbe538/initial] (3) [perdir D:/Programme/wamp/www/] strip per-dir prefix: D:/Programme/wamp/www/de/hello.php -> de/hello.php
127.0.0.1 - - [23/Dec/2008:01:20:36 +0100] [localhost/sid#6b5148][rid#fbe538/initial] (3) [perdir D:/Programme/wamp/www/] applying pattern '^(de¦en)$' to uri 'de/hello.php'
127.0.0.1 - - [23/Dec/2008:01:20:36 +0100] [localhost/sid#6b5148][rid#fbe538/initial] (3) [perdir D:/Programme/wamp/www/] add path info postfix: D:/Programme/wamp/www/de -> D:/Programme/wamp/www/de/hello.php
127.0.0.1 - - [23/Dec/2008:01:20:36 +0100] [localhost/sid#6b5148][rid#fbe538/initial] (3) [perdir D:/Programme/wamp/www/] strip per-dir prefix: D:/Programme/wamp/www/de/hello.php -> de/hello.php
127.0.0.1 - - [23/Dec/2008:01:20:36 +0100] [localhost/sid#6b5148][rid#fbe538/initial] (3) [perdir D:/Programme/wamp/www/] applying pattern '^(de¦en)/(.*)' to uri 'de/hello.php'
127.0.0.1 - - [23/Dec/2008:01:20:36 +0100] [localhost/sid#6b5148][rid#fbe538/initial] (2) [perdir D:/Programme/wamp/www/] rewrite 'de/hello.php' -> 'hello.php/index.php?language=de'
127.0.0.1 - - [23/Dec/2008:01:20:36 +0100] [localhost/sid#6b5148][rid#fbe538/initial] (3) split uri=hello.php/index.php?language=de -> uri=hello.php/index.php, args=language=de
127.0.0.1 - - [23/Dec/2008:01:20:36 +0100] [localhost/sid#6b5148][rid#fbe538/initial] (3) [perdir D:/Programme/wamp/www/] add per-dir prefix: hello.php/index.php -> D:/Programme/wamp/www/hello.php/index.php
127.0.0.1 - - [23/Dec/2008:01:20:36 +0100] [localhost/sid#6b5148][rid#fbe538/initial] (2) [perdir D:/Programme/wamp/www/] strip document_root prefix: D:/Programme/wamp/www/hello.php/index.php -> /hello.php/index.php
127.0.0.1 - - [23/Dec/2008:01:20:36 +0100] [localhost/sid#6b5148][rid#fbe538/initial] (1) [perdir D:/Programme/wamp/www/] internal redirect with /hello.php/index.php [INTERNAL REDIRECT]
127.0.0.1 - - [23/Dec/2008:01:20:36 +0100] [localhost/sid#6b5148][rid#fc7ea8/initial/redir#1] (3) [perdir D:/Programme/wamp/www/] add path info postfix: D:/Programme/wamp/www/hello.php -> D:/Programme/wamp/www/hello.php/index.php
127.0.0.1 - - [23/Dec/2008:01:20:36 +0100] [localhost/sid#6b5148][rid#fc7ea8/initial/redir#1] (3) [perdir D:/Programme/wamp/www/] strip per-dir prefix: D:/Programme/wamp/www/hello.php/index.php -> hello.php/index.php
127.0.0.1 - - [23/Dec/2008:01:20:36 +0100] [localhost/sid#6b5148][rid#fc7ea8/initial/redir#1] (3) [perdir D:/Programme/wamp/www/] applying pattern '^(de¦en)$' to uri 'hello.php/index.php'
127.0.0.1 - - [23/Dec/2008:01:20:36 +0100] [localhost/sid#6b5148][rid#fc7ea8/initial/redir#1] (3) [perdir D:/Programme/wamp/www/] add path info postfix: D:/Programme/wamp/www/hello.php -> D:/Programme/wamp/www/hello.php/index.php
127.0.0.1 - - [23/Dec/2008:01:20:36 +0100] [localhost/sid#6b5148][rid#fc7ea8/initial/redir#1] (3) [perdir D:/Programme/wamp/www/] strip per-dir prefix: D:/Programme/wamp/www/hello.php/index.php -> hello.php/index.php
127.0.0.1 - - [23/Dec/2008:01:20:36 +0100] [localhost/sid#6b5148][rid#fc7ea8/initial/redir#1] (3) [perdir D:/Programme/wamp/www/] applying pattern '^(de¦en)/(.*)' to uri 'hello.php/index.php'
127.0.0.1 - - [23/Dec/2008:01:20:36 +0100] [localhost/sid#6b5148][rid#fc7ea8/initial/redir#1] (1) [perdir D:/Programme/wamp/www/] pass through D:/Programme/wamp/www/hello.php
127.0.0.1 - - [23/Dec/2008:01:20:36 +0100] [localhost/sid#6b5148][rid#fc0540/subreq] (3) [perdir D:/Programme/wamp/www/] strip per-dir prefix: D:/Programme/wamp/www/index.php -> index.php
127.0.0.1 - - [23/Dec/2008:01:20:36 +0100] [localhost/sid#6b5148][rid#fc0540/subreq] (3) [perdir D:/Programme/wamp/www/] applying pattern '^(de¦en)/(.*)' to uri 'index.php'
127.0.0.1 - - [23/Dec/2008:01:20:36 +0100] [localhost/sid#6b5148][rid#fc0540/subreq] (1) [perdir D:/Programme/wamp/www/] pass through D:/Programme/wamp/www/index.php
But unfortunately if I use your solution...
RewriteRule ^(de¦en)/(.*) $2?language=$1 [QSA,L]
I have already set "DirectoryIndex index.php" in my .htaccess.
The log shows the following:
127.0.0.1 - - [23/Dec/2008:01:34:21 +0100] [localhost/sid#6b5148][rid#fcd568/initial] (3) [perdir D:/Programme/wamp/www/] add path info postfix: D:/Programme/wamp/www/de -> D:/Programme/wamp/www/de/
127.0.0.1 - - [23/Dec/2008:01:34:21 +0100] [localhost/sid#6b5148][rid#fcd568/initial] (3) [perdir D:/Programme/wamp/www/] strip per-dir prefix: D:/Programme/wamp/www/de/ -> de/
127.0.0.1 - - [23/Dec/2008:01:34:21 +0100] [localhost/sid#6b5148][rid#fcd568/initial] (3) [perdir D:/Programme/wamp/www/] applying pattern '^(de¦en)$' to uri 'de/'
127.0.0.1 - - [23/Dec/2008:01:34:21 +0100] [localhost/sid#6b5148][rid#fcd568/initial] (3) [perdir D:/Programme/wamp/www/] add path info postfix: D:/Programme/wamp/www/de -> D:/Programme/wamp/www/de/
127.0.0.1 - - [23/Dec/2008:01:34:21 +0100] [localhost/sid#6b5148][rid#fcd568/initial] (3) [perdir D:/Programme/wamp/www/] strip per-dir prefix: D:/Programme/wamp/www/de/ -> de/
127.0.0.1 - - [23/Dec/2008:01:34:21 +0100] [localhost/sid#6b5148][rid#fcd568/initial] (3) [perdir D:/Programme/wamp/www/] applying pattern '^(de¦en)/(.*)' to uri 'de/'
127.0.0.1 - - [23/Dec/2008:01:34:21 +0100] [localhost/sid#6b5148][rid#fcd568/initial] (1) [perdir D:/Programme/wamp/www/] pass through D:/Programme/wamp/www/de
Is there any possibility to take the file index.php as default if there is no other file given?
And I tried the redirect from url with trailing slash to url without trailing slash as the following:
RewriteRule ^(de¦en)$ /$1 [R=301,L]
RewriteRule ^(de¦en)/(.*) $2/index.php?language=$1 [QSA,L]
Unfortunately here my browser (Firefox3) says "redirect-loop".
I also suggest using a canonical URL in the substitution URL of the first rule:
RewriteRule ^(de¦en)$ [i]http://www.example.com[/i]/$1/ [R=301,L]
Jim
To solve the problem when I try to reach different files in addition to "index.php" I tried the following rewrite:
RewriteEngine on
RewriteRule ^(de¦en)$ /$1/ [R=301,L]
//URL-Extension empty or index.php? --> Rewrite to index.php
RewriteRule ^(de¦en)/(^*¦index\.php) index.php?language=$1 [QSA,L]
//Here I need to include an else-case, because the following rule should only be interpreted if the previously doesn't return true
//How do I manage this?
RewriteRule ^(de¦en)/(.*)/(.*) $2/$3?language=$1 [QSA,L]
Unfortunately here I get also Error500 and nothing is listed in the log.
I don't know where I made a mistake and maybe there is any possibility to make these rules better.
Thank you for your help in advance!
Michael
RewriteEngine on
# Externally redirect to add missing trailing slash
RewriteRule ^(de¦en)$ [b]http://www.example.com[/b]/$1/ [R=301,L]
#
# URL-Extension empty or index.php --> Rewrite to index.php
RewriteRule ^(de¦en)/[b](index\.php)?$[/b] index.php?language=$1 [QSA,L]
#
# Rewrite extensions which include one or more subdirectories
RewriteRule ^(de¦en)/[b]([^/]+)[/b]/(.*)$ $2/$3?language=$1 [QSA,L]
Don't confuse regular expressions quantifiers with character-matching tokens. "*" is a quantifier meaning "match zero or more of the preceding character or parenthesized group of characters or subpatterns." See the regex tutorial cited in our forum charter for more info.
Jim
I replaced the last RewriteRule through the following:
RewriteRule ^(de¦en)/[b]?[/b]([^/]+)/[b]?[/b](.*)$ $2/$3?language=$1 [QSA,L]
And to keep the first RewriteRule variable:
RewriteRule ^(de¦en)$ http://[b]%{HTTP_HOST}[/b]/$1/ [R=301,L]
I hope this change doesn't cause some issues?
Now it works perfectly!
Thank you all very much for help and I wish you Merry Christmas!
Michael
[edited by: Fireball22 at 12:28 pm (utc) on Dec. 24, 2008]
Now you need to test it with "unexpected" URL requests and ensure that those are served a 404.
The "open" usage of $3 means it will accept "anything" and "everything", and that may not be desirable.
$3 could contain this or this.that or this/that or this/that.other or this.that/other with or without a trailing slash, including multiple slashes, multiple punctuation and so on.
Using HTTP_HOST as you did is fine, although you should take care to avoid allowing more than one exact hostname to resolve. For example, you should 301 redirect example.com to www.example.com or vice-versa, so that only one can be used to directly-access content. Otherwise, again, you face duplicate-content problems.
Jim
The requested URL /en/hello.php was not found on this server.
Yes you're right!
That means I should only redirect to [%{HTTP_HOST}...] OR only to [%{HTTP_HOST}...] right?
What would you prefer? Are there any advantages or disadvantages?
Michael
# Rewrite all URL-paths in en/ and de/ subdirectories
RewriteRule ^(de¦en)/(.*)$ $2?language=$1 [QSA,L]
A rule that is almost always true: "If you are guessing about the regular-expressions patterns in your rules, then you are creating technical or search ranking problems." An investment now in gaining a solid understanding of regex and what it means in mod_rewrite is a very good investment in the future...
I can't answer your HTTP_HOST question, except to say that the code snippets you posted won't work. The rule has to have some knowledge of which version (www or non-www) is correct. If subdomains in addition to "www" are used, then the rule needs to "know" about them too. The best solution depends on how many domains you have, how many subdomains each of those have, and what the values of their subdomain names are.
Jim
And now I've changed a little bit, so that it doesn't attach any parameters to folders:
RewriteRule ^(de¦en)/(.*)$ $2?language=$1 -f [QSA,L]
RewriteRule ^(de¦en)/(.*)$ $2 -d [L]
But I don't know where these parameters have to be placed?
Do I have to add an extra RewriteCond condition?
I've got my knowledge from:
[translate.google.de...]
I think this solution is the best one, if you try to provide more languages on your sites and to reach, that all search engines takes up your site with the different languages correctly.
Or is this solution not acceptable?
Yes that's of course right!
My domain is reachable with or without www.
The other subdomains are only reachable without www, but there my .htaccess doesn't have an effect.
Michael
*** My domain is reachable with or without www. ***
If both directly serve content, then you have a problem. One should issue a redirect.
[edited by: g1smd at 5:27 pm (utc) on Dec. 24, 2008]
This is not a recipe for success.
I suggest that you stop coding and start defining exactly what you want to do, in terms that mod_rewrite can check and act upon. This would include requested URL-paths, attached query string parameters, HTTP request header values, and file and directory 'exists' checks, as well as a few others. Only once you have a clear goal can you proceed to coding.
This also prevents a lot of wasted time, and too-long threads such as this one.
Get your basic knowledge from Apache [httpd.apache.org], not from other Web sites or forums.
Jim
[edited by: jdMorgan at 7:08 pm (utc) on Dec. 24, 2008]
# 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]
As shown, the rule accepts (does not redirect) requested hostnames in the form www.<any-domain><.com, .de, .info, .museum> or <.co.uk or co.de> with or without appended (FQDN) periods or port numbers. Because of this latter point, you might want to add yet another rule after this one:
# Externally redirect to remove trailing period or port
# number on requested subdomain (www or other) hostnames
RewriteCond %{HTTP_HOST} ^([^.:]+\.[^.:]+\.(co\.[a-z]{2}¦[a-z]{2,6}))(\.¦\.?:[0-9]+)$
RewriteRule (.*) http://%1/$1 [R=301,L]
Jim
That's a very interesting possibility from you!
But why do you exactly test all the several domain-levels and not only if the 3rd-domain-level (www) exists?
Is it not recommend to leave the port-number or trailing period?
#Externally redirect; Add "www"
RewriteCond %{HTTP_HOST} !^www\.(.+)$
#I thought (.*) saves the hole requested address and not only the trailing period such as GET parameters and so on?
RewriteRule (.*) http://www.%{HTTP_HOST}/$1 [R=301,L]
Michael
# 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]
Michael