Forum Moderators: phranque
hopefully one of you could help me with the following problem. Just to mention it, I'm not a programmer. The website is driven by a nuke-system with a lot of different modules, but only a few have been rewritten with mod_rewrite upon today. However, the site is multilingual, and so the URL should always show the selected language as a subdirectory name.
O.k. here is the problem:
Original URL like [mydomain.com...]
should be [mydomain.com...]
The subdir /english should remain, as long as the user changes the language.
I tried the following from .htaccess:
RewriteEngine On
RewriteBase /test
#Index
RewriteRule ^index.html index.php
RewriteCond %{HTTP_COOKIE} ^lang=(.+)$
RewriteRule ^/(.+)/index.html [mydomain.com...]
But that hasn't brought the result I'm looking for.
So I tried instead of RewriteCond and RewriteRule from above the following line:
RewriteRule ^([a-z]*)/index.html index.php?newlang=$1
What works for the index.html but not for all of the other modules and even not for the CSS file.
Well, it might also be a problem of the php programming. Just because I'm not sure about this, here are the line's from my header.php file:
ob_start();
function replace_for_mod_rewrite(&$s)
{
$urlin = array(
"'(?<!/)index.php'",
"'(?<!/)index.php\?newlang=([a-z]*)'",
"'(?<!/)modules.php\?name=Home'",
"'(?<!/)modules.php\?name=Terms_Of_Use'",
"'(?<!/)modules.php\?name=Disclaimer'",
"'(?<!/)modules.php\?name=Privacy_Policy'",
"'(?<!/)modules.php\?name=Solutions'",
"'(?<!/)modules.php\?name=Solutions&op=([a-z]*)'",
"'(?<!/)modules.php\?name=Subscribe'",
"'(?<!/)modules.php\?name=Contact'",
"'(?<!/)modules.php\?name=Recommend_Us'",
"'(?<!/)modules.php\?name=Shopping'",
"'(?<!/)modules.php\?name=FAQ'",
"'(?<!/)modules.php\?name=FAQ&file=index'",
"'(?<!/)modules.php\?name=FAQ&myfaq=([a-zA-Z0-9+]*)&id_cat=([0-9]*)&categories=([a-zA-Z0-9+]*)'",
"'(?<!/)modules.php\?name=Surveys'",
"'(?<!/)modules.php\?name=Surveys&pollID=([a-zA-Z0-9]*)'",
"'(?<!/)modules.php\?name=Surveys&op=results&pollID=([a-zA-Z0-9]*)'",
);
$urlout = array(
"index.html",
"\\1\\index.html",
"\\1\\home.html",
"terms-of-use.html",
"disclaimer.html",
"privacy-policy.html",
"products-and-services.html",
"products-and-services-\\1.html",
"subscribe.html",
"contact.html",
"recommend-us.html",
"shop.html",
"faq.html",
"faq.html",
"faq-\\1-\\2-\\3",
"surveys.html",
"survey\\1.html",
"survey\\1results.html"
);
$s = preg_replace($urlin, $urlout, $s);
return $s;
}
Anyway I have read lot's of manuals and postings now, but didn't found any solution, but I guess it's just a simple problem.
If someone has tips for me what to do, I would be very glad. Thanks a lot.
bibu
Welcome to WebmasterWorld [webmasterworld.com]!
I'm not sure I understand your question. For example, I see some mention of HTTP_COOKIE, but no explanation of how you might use it in your site. Also, I'm not very good at reading PHP.
I assume that you want to take all URLs with ".html" extensions, and rewrite them to your php script, with a query string variable taking the language value of the requested subdirectory. The script can then access the name of the requested page (if needed) using the REQUEST_FILENAME system variable or similar.
In that case, you might try:
RewriteRule ^([^/]+)/.+\.html /index.php?newlang=$1 [L]
Browser request --> Request passed to your script http:example.com/english/index.html --> http://example.com/index.php?newlang=english http:example.com/francais/sample.html --> http://example.com/index.php?newlang=francais http:example.com/index.html --> NO CHANGE (no language subdir in URL)
http:example.com/francais/index.html --> http://example.com/index.php?newlang=francais
http:example.com/deutch/index.html --> http://example.com/index.php?newlang=deutch
http:example.com/deutch/main.css --> NO CHANGE (not an html page)
http:example.com/english/logo.jpg --> NO CHANGE (not an html page)
http:example.com --> NO CHANGE (no language subdir & not an html page)
http:example.com/ --> NO CHANGE (no language subdir & not an html page)
Jim
thanks for your reply. I tried your suppestion, but it
redirects me to my real homepage. So I adjusted your line by adding the subdir /test/index.php?newlang=$1 [L], but this brings the same results as my line:
RewriteRule ^([a-z]*)/index.html index.php?newlang=$1
That means it shows my wish-URL as [mydomain.com...] but that's all. Any other site will bring an error 404.
When going to my navigation menu and selecting another site, e.g. privacy-policy.html it is shown as URL in the browser address bar, but the site is index.html
As I showed in my first posting, I tried to get the language from the Cookie. The Cookie is set when entering the site and will be updated when language is changed. The selected website language is taken from the cookie, so that the system shows the correct pages. So it seems to me that I have to get the cookie language into all of this.
Hm, but for tonight I finish my testing. Thanks again, and hopefully you could direct me into the right direction.
bibu
By the way: in your example urls you have stated /deutch, but should be /deutsch. Just missed an "s". I'm one of these deutsch-people ;-)
no problem, that your spelling was wrong. I'm even not an expert of english and german is a real difficult
language. I know this ;-)
O.k. as you suggested, here is what is in my htaccess:
(I have left my comments in it)
DirectoryIndex index.php index.cgi index.pl index.shtml index.html
<Limit GET PUT POST>
order allow,deny
deny from 80.179.128.
deny from 64.140.49.
deny from 200.
allow from all
</Limit>
# deny most common except .php
<FilesMatch "\.(inc¦tpl¦h¦ihtml¦sql¦ini¦conf¦class¦bin¦spd¦theme¦module¦exe)$">
deny from all
</FilesMatch>
#Disable .htaccess viewing from browser
<Files ~ "^\.ht">
Order allow,deny
Deny from all
Satisfy All
</Files>
<Files ~ "\config.php$">
deny from all
</Files>
#ErrorDocument 404 /test/error404.html
RewriteEngine On
RewriteBase /test
#Index
RewriteRule ^index.html index.php
#RewriteCond %{HTTP_COOKIE} ^lang=(.+)$
#RewriteRule ^/(.+)/index.html [mydomain.com...]
RewriteRule ^([^/]+)/.+\.html /test/index.php?newlang=$1 [L]
#RewriteRule ^([a-z]*)/index.html index.php?newlang=$1
#([a-z]*)/ in front of each RewriteRule html page works only for english
#Home
RewriteRule ^home.html modules.php?name=Home
#RewriteRule ^([a-z]*)/home.html modules.php?name=Home
#above line only works with 'english', but not with 'german'
#Solutions
RewriteRule ^products-and-services.html modules.php?name=Solutions
RewriteRule ^products-and-services-([a-z]*).html modules.php?name=Solutions&op=$1
#Subscribe
RewriteRule ^subscribe.html modules.php?name=Subscribe
#RewriteRule ^([a-z]*)/subscribe.html modules.php?name=Subscribe
#above line only works with 'english', but not with 'german'
#Contact
RewriteRule ^contact.html modules.php?name=Contact
#Recommend Us
RewriteRule ^recommend-us.html modules.php?name=Recommend_Us
#Shopping
RewriteRule ^shop.html modules.php?name=Shopping
#Terms of Use
RewriteRule ^terms-of-use.html modules.php?name=Terms_Of_Use
#Privacy Policy
RewriteRule ^privacy-policy.html modules.php?name=Privacy_Policy
#Disclaimer
RewriteRule ^disclaimer.html modules.php?name=Disclaimer
#FAQ
RewriteRule ^faq.html modules.php?name=FAQ
RewriteRule ^faq.html modules.php?name=FAQ&file=index
RewriteRule ^faq-([a-zA-Z0-9+]*)-([0-9]*)-([a-zA-Z0-9+]*) modules.php?name=FAQ&myfaq=$1&id_cat=$2&categories=$3
#RewriteRule ^faq-([a-zA-Z0-9+]*)-([0-9]*)-([a-zA-Z0-9+]*) modules.php?op=modload&name=FAQ&file=index&myfaq=$1&id_cat=$2&categories=$3
#Surveys
RewriteRule ^surveys.html modules.php?name=Surveys
RewriteRule ^survey([a-zA-Z0-9]*).html modules.php?name=Surveys&pollID=$1
RewriteRule ^survey([a-zA-Z0-9]*)results.html modules.php?name=Surveys&op=results&pollID=$1
End of htaccess-
Well, everything works fine, as long as I don't try to get the language shown as
a subdirectory in the URL. The language is set at the first visit to the site, all other
sites visited then, don't need the language-variable, because it is taken from the cookie
and it is a global variable of php.
Here are some examples of what I like to achieve:
When entering the site, the language is english. A cookie is set with this language. The URL will be
[mydomain.com...]
Hm, maybe it would be better to show it directly as [mydomain.com...] which
is originally [mydomain.com...]
All other site will then been show as:
[mydomain.com...] is [mydomain.com...]
[mydomain.com...] is [mydomain.com...]
[mydomain.com...] is [mydomain.com...]
and so on.
When changing the language to german, it all will look like:
[mydomain.com...] is [mydomain.com...]
[mydomain.com...] is [mydomain.com...]
[mydomain.com...] is [mydomain.com...]
[mydomain.com...] is [mydomain.com...]
and so on. The language is taken form the cookie or the php variable.
Hopefully this explains a bit better what I like to have as result. Thanks for your help.
stephan alias bibu
Well, I still can't see anything obviously wrong, but one thing that will cause problems is your URL structure. By having the language as the first-level subdirectory, it is in the same postition in the URL as your "real" php files and subdirectories. Therefore, it is difficult to determine whether the first-level subdirectory name is an actual file or if it is just a language to be rewritten. I would strongly suggest you consider using subdomains instead of subdirectory names for you language options, that is: http://www.mydomain.de.com for deutsch and http://mydomain.en.com for english. This will remove the ambiguity and make your rewriting much simpler.
Another thing that will simplify your site is to keep the hyphens and capitalization consistent between your URLs and your script parameters. For example, your rule
#Terms of Use
RewriteRule ^terms-of-use.html modules.php?name=Terms_Of_Use
#Terms of Use
RewriteRule ^terms-of-use.html modules.php?name=terms-of-use
RewriteRule ^(.+)\.html modules.php?name=$1
If you use this method, then Contact, Recommend Us, Shopping, Terms of Use, Privacy Policy, Disclaimer, and all other 'simple' URLs can be handled by a single rule.
As a matter of fact, I'd suggest you do most of your query manipulations inside your modules script, rather than using mod_rewrite.
If you do this, I think you can reduce your rules to three:
# Get language from cookie, put into query string for index.html only
RewriteCond %{HTTP_COOKIE} ^lang=(.+)$
RewriteRule ^index\.html /index.php?newlang=%1 [L]
# language already selected, rewrite index.html to index.php with no language change
RewriteCond %{REQUEST_URI} ^/(english¦german)
RewriteRule ^[^/]/index\.html /index.php? [L]
# For all other pages (not index.html), rewrite to modules.php
RewriteCond %{REQUEST_URI} ^/(english¦german)
RewriteRule ^[^/]/(.+) /modules.php?name=$1 [L]
I'm sorry that's the best I can do, but you site's architecture is not obvious to me, nor is the "language assignment" process. All I can really do is recommend that you use the simplest and most regular design possible. Check your site error log when you get an error, and enable mod_rewrite logging if you have permissions to do so.
Note that posting on this board changes the solid pipe character into a broken pipe "¦" character. You will have to edit the pipes above to use the solid pipe character.
Always use the [L] flag unless you need to do further rewrites to the URL in the current HTTP request.
Jim
thanks for your last reply. The idea of subdomains is something I never thought about, but sounds good to me.
However, I will need to try the things you recommended. So, for the moment I thank you very much, I appreciate your assistance.
As soon as I have tried what you stated in your last post, I will give you a feedback within this thread.
Thanks again.
Stephan