Forum Moderators: phranque

Message Too Old, No Replies

mod rewrite - Subdomain to Get string

Passing the subdomain name to the $_GET array.

         

jonter

4:57 pm on May 26, 2008 (gmt 0)

10+ Year Member



Hey,
I've been searching the forums but I haven't been able to find the exact solution to my problem.

I'm running a quite large multi language web site. At the moment the language of the site is determined from the path of the URI. E.g. http://www.example.com/en/what/ever.html would show an English site.

.htaccess:


RewriteEngine on
RewriteRule ^([a-z][a-z])/?$?lang=$1 [L]
RewriteRule ^([a-z][a-z])/([a-z_]+)/?$?lang=$1&page=$2 [L]
RewriteRule ^([a-z][a-z])/([a-z_]+)/(.+).html?$?lang=$1&page=$2&part=$3 [L]

Now I'm looking to rewrite the .htaccess file to be able to pass the language parameter to sub domain level (wikipedia stylish). Such as [en.example.com...] I'm thinking it should be something like this, however it's not fully working out for me.

.htaccess:


RewriteEngine On
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com
RewriteRule (.*) ?lang=%1&theuri=$1 [L]

Any help would be much appreciated!

Jon

jdMorgan

6:49 pm on May 26, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Is this what you mean?

RewriteEngine on
#
RewriteCond %{HTTP_HOST} ^([a-z]{2})\.example\.com
RewriteRule ^([a-z_]+)/?$ /?lang=%1&page=$1 [L]
#
RewriteCond %{HTTP_HOST} ^([a-z]{2})\.example\.com
RewriteRule ^([a-z_]+)/([^/.]+)\.html?$ /?lang=%1&page=$1&part=$2 [L]

Note that by making the trailing slash optional in your first rewrite pattern and the "l" on "html" optional in your second, you have created a duplicate-content issue. You should pick one or the other URL format, always use that format in your links, and externally redirect the other format version to it. This will clear up any "mixed" search engine results, and result in one URL for one page, instead of two URLs leading to the same page.

The same goes for the non-www and www version of your domain (and subdomains), and domains and subdomains with or without trailing dots or port numbers -- Redirect all non-canonical hostnames to the canonical hostname. For example, you might get a request for http://www.en.example.com.:80/somepage.html. That is a perfectly-valid request (try it with a real page name), but should be externally redirected to http://en.example.com/somepage.html before internally rewriting the request to your script.

Jim

jonter

7:16 pm on May 26, 2008 (gmt 0)

10+ Year Member



Jim,
Thanks a lot for your input. About the ".html?" thanks for pointing that out, of course that should be just ".html". Wow.

Your rewrite code looks just as what I'm looking for. However it gives me a "500 Internal Server Error" and I'm pretty certain that my hosting company are to blame (I don't know if I'm allowed to mention them, they're one.com, any mods feel free to remove that if I'm breaking the TOS). I found this in their FAQ:

"To set up a sub-domain, you should add a directory to the root for your web hotel. There should be an index file in the directory."

So... I suppose they are messing with something similar at a lower level, supposedly httpd.conf. I'm not sure if I can override that.

I'M STUCK! :´-(
Jon

[edit: Getting 500, not 404 as previously stated. Sorry]

jdMorgan

7:39 pm on May 26, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The code above is only part of the solution. It rewrites URLs to a different filepath than what you had before.

You must implement the new file structure so that the rewritten filepaths are valid. This includes any subdomain-to-filespace mapping needed to resolve the requested (and rewritten) URL to a file.

From your description, it sounds like you need to investigate the capabilities of your "control panel" and find out what options are available for mapping domains and/or subdomains into your account's filespace.

In order to use the code above, you'll need to map all subdomains to your original domain's filespace. This may or may not be possible with your particular control panel. If it isn't, then you won't be able to use that code, and instead will have to take a different approach -- possibly using *nix symlinks from the subdomains' filespaces back to the main domain's filespace, so that your script "appears" to exist in each of your subdomains' filespaces.

Jim

jonter

7:53 pm on May 26, 2008 (gmt 0)

10+ Year Member



I found out that the code you gave me only gives 500 for [en.example.com...] while it gives 404 for [en.example.com...] or [en.example.com...]

I suppose I would need a rule to match the main page of a certain sub domain.

Anyway that's not the problem at the moment. I will look closer at the different possible solutions you mentioned.

Thanks,
Jon

jdMorgan

11:06 pm on May 26, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Both rules above require a non-blank URL-path in order to take action, so they are not responsible for the 500-Server Error on a request for "en.example.com". Check your server error log to find the reason for the 500 error.

Jim