Forum Moderators: phranque

Message Too Old, No Replies

URL language directory rewrite => query string add-on

How to rewrite some url language directory rewrite to a query string add-on

         

RewriteEngine

12:13 pm on Jun 26, 2007 (gmt 0)

10+ Year Member



I have spend quite some time reading the Apache URL Rewriting Guide and searching for sample code, but it seems that I'm not smart enough to solve this basic .htaccess problem

I'm trying to rewrite fr/es/en language directories and add them to the query string (as &langauge=fr or?language=fr):
mysite.com/fr/index.php
=> mysite.com/catalog/index.php?language=fr
and
mysite.com/fr/index.php?cPath=22
=> mysite.com/catalog/index.php?cPath=22&language=fr

This is how far I've gotten so far:
RewriteEngine on
RewriteBase /
RewriteRule ^([en¦es¦fr]+)/(.*)$ /catalog/$2?language=$1 [L]

Can someone point me to some sample code / documentation on this specific situation or maybe share some advice on where I'm not getting this right.

jdMorgan

2:23 pm on Jun 26, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This is mostly a regular expressions problem; You've used a character [group] inappropriately. The group '[abc]' will match the single character 'a' or 'b' or 'c'. If you change that to '[abc]+', then it will match one or more occurrences of 'a' or 'b' or 'c'.

The pipe character within a group is interpreted literally, so you've told the regex parser to match any URL-path starting with one or more characters equal to 'e', 'n', '¦', 's', 'f', or 'r', followed by a slash, followed by anything or nothing.

In addition, specifying a new query string will replace any existing character string in the client request by default. In order to retain your 'cPath' name/value pair, you'll need to use [QSA] -- the Query String Append flag, to append the new 'language' name/value pair to the existing 'cPath' name/value pair if present.

So, you may have better luck with something like this:


RewriteRule ^(en¦es¦fr)/(.*)$ /catalog/$2?language=$1 [QSA,L]

Replace the broken pipe "¦" characters with solid pipe characters before trying to use this code; Posting on this forum modifies the pipe characters.

There may be other problems, but this should eliminate at least two of them.

For more information, see the documents cited in our forum charter [webmasterworld.com] and the tutorials in the Apache forum section of the WebmasterWorld library [webmasterworld.com].

Jim

superpelo

5:58 pm on Jun 26, 2007 (gmt 0)

10+ Year Member



Hi,
excuse my bad english.

I have had the same problem that you had and I have solved it with this code:
DirectoryIndex index.html index.htm index.php
ErrorDocument 404 /404.php

RewriteEngine On
RewriteBase /

RewriteRule ^(it¦en)/file/site/image/(menu¦titoli)/(.+)$ file/site/image/$1/$2/$3 [L,NC]
RewriteRule ^(it¦en)/(.+)$ $2?l=$1 [QSA,NC,L]
RewriteRule ^(it¦en)/$ index.php?l=$1 [NC,L]

I still have a problem: i would like that if you type [domain.it...] you will receive a 404 error.

In other words i need a Rules that sounds like this:
if (!ereg('/it/',URL) &&!ereg('/en/',URL) {
goTo(myErrorPage.php);
}

If URL don't contains '/it/' and don't contains '/en/' web server must show a determinate page.

I hope you can understand me..

Thanks, Bye.

RewriteEngine

8:26 pm on Jun 29, 2007 (gmt 0)

10+ Year Member



Ty so much jdMorgan, and thank you for explaining it in detail so. I was also glad to see that I wasn't totally off track :-) Your help was great, and once uploaded I have to admit I did some spontanous dancing of joy. Cool stuff that works is a feet mover :)

superpelo, I am sorry but I don't know how to do what you are requestion. I think I read in here somewhere that you can set up a redirect to something that doesn't exist, as that will trigger your 404 rule, or you can do as I have done a number of times - create a 404 http status code with you php script: header("HTTP/1.1 404 Not Found");

jdMorgan

1:21 am on Jun 30, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, and we try to stick to the original poster's problem in a single thread as well...

superpelo,

It's not entirely clear *where* in the URL the "it" or "en" should be found, but based on your existing rules, I think you're looking for something like this:


RewriteCond %{THE_REQUEST} !^[A-Z]{3,9}\ /(it¦en)
RewriteRule .* /path-to-file-that-does-not-exist.xyz [L]

By using THE_REQUEST, we look at only what the browser requested. This rule *will not* rewrite the URLs created by your rule posted above that have no "it" or "en" at the beginning of the URL-path.

However, it will rewrite all other requests without "it" or "en" at the beginning to a filepath that is invalid -- it does not exist. So, this triggers your server's 404 response handler.

Replace the broken pipe "¦" character with a solid pipe character before use; Posting on this forum modifies the pipe characters.

Jim

[edited by: jdMorgan at 3:40 pm (utc) on June 30, 2007]