views:

94

answers:

1

I use the following pattern:

http://example/index.php?lang=en
http://example/index.php?lang=es
http://example/index.php?lang=zh-tw
http://example/index.php?lang=zh-cn

And I use the following mod_write rule:

RewriteEngine on
RewriteRule ^([a-z]{2}(-[A-Z]{2})?)/(.*) $3?lang=$1 [L,QSA]

With that rule I can use the following URLs which redirect me to the URLs above (the original ones):

http://example/en/index.php
http://example/es/index.php
http://example/zh-cn/index.php
http://example/zh-tw/index.php

I wish I could just type:

http://example/en
http://example/es
http://example/zh-cn
http://example/zh-tw

To access the index pages...

and type this:

http://example/en/thanks
http://example/es/thanks
http://example/zh-cn/thanks
http://example/zh-tw/thanks

to enter a thanks.php file I have.

Any suggestions to get that result?

(I think this should be quite common. Almost every professional website has pages in this fashion: example/section).

+1  A: 

If I understood what you were looking for correctly, the following should do what you want (my rules make the assumption that you're using the root of a domain):

RewriteEngine on

# Go to index.php by default if nothing is specified after the language
RewriteCond %{REQUEST_URI} ^/[a-z]{2}(-[A-Za-z]{2})?/?$
RewriteRule ([^/]*)/?$ index.php?lang=$1

# Rewrite domain.com/lang/index to domain.com/lang
RewriteRule ^([a-z]{2}(-[A-Z]{2})?)/index(\.php)?$ $1 [R,QSA]

# Allow pages to be specified without the .php extension
# The RewriteCond tells mod_rewrite to not rewrite if the request is a real file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([a-z]{2}(-[A-Z]{2})?)/(.*)(\.php)? $3.php?lang=$1 [L,QSA]

If you are not at the root of the domain (ie. your URLs are domain.com/subdomain/en/index), you need to specify RewriteBase /subdomain and modify your RewriteCond slightly (you'd put the rules in a .htaccess file in the subdirectory):

RewriteCond %{REQUEST_URI} ^/subdomain/[a-z]{2}(-[A-Za-z]{2})?/?$

If you're still having trouble getting your CSS to load, try using absolute paths. The relative path will apply after the redirect.

Daniel Vandersluis
@Daniel Vandersluis Thanks it almost worked but when I enter: http://alex-chen.net/test/en/thanks en, and es are displayed but with no CSS style and zh-tw and zh-cn can't be found!(I used your code above just after my code I mention up there).
janoChen
My rules should be instead of yours, not in addition too (my last rule covers the rule that you had in your question). You will probably want to add a rule so that css, js, images, etc. aren't rewritten (something like `RewriteCond \.(css|js|jpe?g|png)$` `RewriteRule ^(.*)$ $1 [L,QSA]`)
Daniel Vandersluis
I updated my first rule to fix the issue you were having with zh-cn too.
Daniel Vandersluis
@Daniel Vandersluis Weird, it works for alex-chen.net/en. But when you type alex-chen.net/en/thanks the page is shown with no CSS. Then if I use the code you provided: RewriteCond \.(css... it says internal server error.
janoChen
Full htaccess: `RewriteEngine on# Don't rewrite css, js, jpg or png files RewriteCond \.(css|js|jpe?g|png)$ RewriteRule ^(.*)$ $1 [L,QSA])# Go to index.php by default if nothing is specified after the languageRewriteCond %{REQUEST_URI} ^/([a-z]{2}(-[A-Z]{2})?)/?$RewriteRule ([a-z-]{2,4})/?$ index.php?lang=$1 [QSA]# Rewrite domain.com/lang/index to domain.com/langRewriteRule ^([a-z]{2}(-[A-Z]{2})?)/index(\.php)? $1 [R,QSA]# Allow pages to be specified without the .php extensionRewriteRule ^([a-z]{2}(-[A-Z]{2})?)/(.*)(\.php)? $3.php?lang=$1 [L,QSA]`
janoChen
Probably better to paste a big block of code like that somewhere else, the formatting is not maintained as a comment. What is the full URL for your CSS file?
Daniel Vandersluis
@Daniel Vandersluis Here it is: http://pastebin.com/KjTuxprn.My CSS are in /styles and my Javascript in /scripts.
janoChen
I've updated once again. The .htaccess as given works for me. As mentioned in my edit, you probably will want to make sure you use absolute paths for your CSS/JS.
Daniel Vandersluis
@Daniel Vandersluis Sorry, how do I write the rule if my CSS are in /styles and Javascript in /scripts and pictures in /images? (I sued the onw in your first comment (comment number 2) but I says Internal Server Error. (Right now is commented)
janoChen
I mean in your HTML: `<link rel="stylesheet" type="text/css" href="http://yourdomain.com/styles/style.css">`, etc.
Daniel Vandersluis
@Daniel Vandersluis It is like this: <link rel="stylesheet" type="text/css" href="styles/global.css" />
janoChen
Right, so just change `styles/global.css` to the full URL (`http://alex-chen.net/test/styles/global.css` or whatever)
Daniel Vandersluis
@Daniel Vandersluis Weird I did this: <link rel="stylesheet" type="text/css" href="http://alex-chen.net/styles/global.css" />and http://alex-chen.net/en/thanks still with no CSS nor Javscript.(I deleted the /test folder, I'm not using it anymore).
janoChen
@Daniel Vandersluis never mind now it works thanks! But is there a way of doing this without changing my /style path in my thanks.php file (which is in my root folder with index.php)?
janoChen
You can set up a rule to redirect files ending in .css to /styles/$1 or something along those lines.
Daniel Vandersluis