Forum Moderators: phranque

Message Too Old, No Replies

mod rewrite: Error500

         

Fireball22

11:58 am on Dec 21, 2008 (gmt 0)

10+ Year Member



Hello,

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

Second solution:

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

Caterham

3:12 pm on Dec 21, 2008 (gmt 0)

10+ Year Member



Check, if that fits your issue.

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]

g1smd

4:59 pm on Dec 21, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I don't see how the usage of $2 fits the URL patterns that were described.

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?

Fireball22

11:46 pm on Dec 21, 2008 (gmt 0)

10+ Year Member



@Caterham

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

Caterham

4:38 am on Dec 22, 2008 (gmt 0)

10+ Year Member



Did you copy or type that code? If you copied it, make sure you change the ¦ pipe character into a solid one from your keyboard. Anyway, if /en/foo should go to /foo/index.php, use the following instead

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.

Fireball22

1:02 pm on Dec 22, 2008 (gmt 0)

10+ Year Member



Thank you very much for your help, now it works fine!

But unfortunately I don't know why we need this redirect in the first RewriteRule line.
Does we need this rule only because of the trailing slash?

Michael

Caterham

1:48 pm on Dec 22, 2008 (gmt 0)

10+ Year Member



Does we need this rule only because of the trailing slash?

Yes, you should use a homogeneous url layout (don't make both /en and /en/ available but redirect one to the other).

g1smd

7:17 pm on Dec 22, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Redirect from URL with trailing slash to URL without trailing slash.

Fireball22

12:39 am on Dec 23, 2008 (gmt 0)

10+ Year Member



Okay, I know what you mean!

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]

... it doesn't work.

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".

jdMorgan

3:54 pm on Dec 23, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Replace the broken pipe "¦" characters with solid pipes before use; Posting on this forum modifies the pipe character, and the code won't work with broken pipes in the regular-expressions patterns.

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]

This will prevent problems on servers where UseCanonicalName is 'on', but the defined ServerName points to the domain name "example.com" while the site is actually using "www.example.com" or vice-versa.

Jim

Fireball22

11:31 pm on Dec 23, 2008 (gmt 0)

10+ Year Member



Thank you very much for your advice!
You're right, that's a very good idea!

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

jdMorgan

12:31 am on Dec 24, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The "else" function follows implicitly, both because the previous rule has an [L] flag, and because the patterns are mutually-exclusive. Note correction to invalid syntax in "index.php or blank" pattern, and second warning to use canonical URL in redirects.

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]

Again, replace broken pipe "¦" characters with solid pipes before use.

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

Fireball22

12:22 pm on Dec 24, 2008 (gmt 0)

10+ Year Member



Thank you very much for your answer and detailed explanation!

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]

g1smd

12:57 pm on Dec 24, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



*** Now it works perfectly! ***

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.

jdMorgan

12:58 pm on Dec 24, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Why did you add the question marks after the two slashes in that rule? Doing so is not advisable, as it makes the pattern ambiguous and creates duplicate content -- The same 'page' is now available at four URLs.

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

Fireball22

1:34 pm on Dec 24, 2008 (gmt 0)

10+ Year Member



Unfortunately If I don't add these question marks I get Error404 if I type in www.example.com/en/hello.php:

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

jdMorgan

1:51 pm on Dec 24, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If it was your intent to rewrite all URLs, then you should first change the comment line so that it is accurate, and then change the code to match the comment:

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

If you get in the habit of putting meaningful and precisely-accurate comments in your code, then you will have much better long-term success with mod_rewrite and far, far, fewer unexpected technical and search ranking problems.

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

Fireball22

4:13 pm on Dec 24, 2008 (gmt 0)

10+ Year Member



Great, this works perfectly!

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

g1smd

5:17 pm on Dec 24, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Your new rules can't possibly work, especially the second one, because all URLs are matched by the first rule every time.

*** 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]

jdMorgan

5:27 pm on Dec 24, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Again, you are showing us code without comments, and without any clear definition of what you want to achieve.

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]

Fireball22

1:19 pm on Dec 26, 2008 (gmt 0)

10+ Year Member



Thank you for your advice!

Now I will first read this documentation directly from Apache.
I think the documentation which I read wasn't detailed enough.

Michael

Fireball22

3:35 pm on Dec 28, 2008 (gmt 0)

10+ Year Member



Now I am trying to redirect all requests without "www" to the adress with "www".

But I would like to keep my domain-name variable, but I don't know how I sould realize this.


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

Michael

g1smd

5:20 pm on Dec 28, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



In the rule, collect up
(.*)
on the left and apply it as
/$1
on the right.

I don't know what you mean about "keeping the domain name variable".

jdMorgan

6:10 pm on Dec 28, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Something like this, perhaps:

# 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]

This should be your very last redirect, following all other redirects, and preceding your internal rewrites.

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]

Replace the broken pipe "¦" characters in both RewriteCond patterns with solid pipe characters before use; Posting on this fourm modifies the pipe characters.

Jim

Fireball22

12:32 am on Dec 29, 2008 (gmt 0)

10+ Year Member



Thank you very much for reply!

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

g1smd

12:49 am on Dec 29, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Trailing port number and/or trailing period is Duplicate Content - a non-canonical URL for the same content. It is best redirected.

You can't test for www as the third level, because with a .co.uk domain the www is at the fourth level (if you are counting periods from the end).

Fireball22

12:12 pm on Dec 29, 2008 (gmt 0)

10+ Year Member



Thank you very much for reply!

You're right, so I will also redirect such requests with port numbers and/or trailing periods.

Michael

Fireball22

3:51 pm on Dec 29, 2008 (gmt 0)

10+ Year Member



I have just tried the following solution from jdMorgan, but unfortunately Apache redirects me to www.www.example.com if I type in a adress with/without "www".


# 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

jdMorgan

4:15 pm on Dec 29, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Did you replace the broken pipe as warned-about above?

Jim

Fireball22

4:25 pm on Dec 29, 2008 (gmt 0)

10+ Year Member



Excuse my mistake. The failure was caused by another script which was included at the same time.

Now it works perfectly!
Thank you very much for your professional help!

Michael