transparencia Posted September 2, 2010 Share Posted September 2, 2010 I want to append a ?lang=$language_code on all the .php pages and directories of the website and I would like to rewrite it like this: site.com/language => site.com/index.php?lang=language site.com/language/dir/ => site.com/dir1.php?lang=language I already have this code in .htaccess: Options -MultiViews RewriteEngine On RewriteBase /pt/ RewriteRule ^product/(.*).html$ products.php?q=$1&rewrite=1&%{QUERY_STRING} [L] RewriteRule ^review/(.*).html$ reviews.php?q=$1&rewrite=1&%{QUERY_STRING} [L] RewriteRule ^merchant/$ merchants.php RewriteRule ^merchant/(.*)/$ search.php?q=merchant:$1:&rewrite=1&%{QUERY_STRING} [L] RewriteRule ^merchant/(.*)/(.*).html$ search.php?q=merchant:$1:&page=$2&rewrite=1&%{QUERY_STRING} [L] RewriteRule ^category/$ categories.php RewriteRule ^category/(.*)/$ search.php?q=category:$1:&rewrite=1&%{QUERY_STRING} [L] RewriteRule ^category/(.*)/(.*).html$ search.php?q=category:$1:&page=$2&rewrite=1&%{QUERY_STRING} [L] Is it possible to do this? Quote Link to comment https://forums.phpfreaks.com/topic/212337-is-it-possible-to-rewrite-this/ Share on other sites More sharing options...
wildteen88 Posted September 2, 2010 Share Posted September 2, 2010 Try the following rules RewriteRule ([a-z]+)/([a-z0-9_-]+)/? $2.php?lang=$1 [NC,L] RewriteRule ([a-z]+)/? index.php?lang=$1 [NC,L] RewriteRule ([a-z]+)/([a-z0-9_-]+)/? $2.php?lang=$1 [NC,L] Will map site.com/en/dir/ to site.com/dir.php?lang=en RewriteRule ([a-z]+)/? index.php?lang=$1 [NC,L] Will map site.com/en/ to site.com/index.php?lang=en Quote Link to comment https://forums.phpfreaks.com/topic/212337-is-it-possible-to-rewrite-this/#findComment-1106353 Share on other sites More sharing options...
transparencia Posted September 2, 2010 Author Share Posted September 2, 2010 That is not working properly. It is not allowing the .css and .jpg to be shown and also it just stays in the index.php even if click on another link. Is it because of the other rewrite rules? Quote Link to comment https://forums.phpfreaks.com/topic/212337-is-it-possible-to-rewrite-this/#findComment-1106359 Share on other sites More sharing options...
wildteen88 Posted September 2, 2010 Share Posted September 2, 2010 Change the rewrite rules to RewriteRule ^([a-z]{2})/([a-z0-9_-]+)/?$ $2.php?lang=$1 [NC,L] RewriteRule ^([a-z]{2})/?$ test.php?lang=$1-89 [NC,L] I have limited the first match to only two characters. You may also want to add these lines RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d After this line RewriteEngine On Quote Link to comment https://forums.phpfreaks.com/topic/212337-is-it-possible-to-rewrite-this/#findComment-1106415 Share on other sites More sharing options...
cags Posted September 2, 2010 Share Posted September 2, 2010 You may also want to add these lines RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d After this line RewriteEngine On Doing so would seem fairly pointless as the conditions would only apply to the first RewriteRule encountered, i.e. RewriteRule ^product/(.*).html$ products.php?q=$1&rewrite=1&%{QUERY_STRING} [L] In this situation I'd suggest inverting those conditions and 'exiting' if the file/directory does exists. Thus giving you the following code to be placed in the same place that wildteen88 mentioned. RewriteCond %{REQUEST_FILENAME} -f [OR] RewriteCond %{REQUEST_FILENAME} -d [OR] RewriteRule ^ - [L] Quote Link to comment https://forums.phpfreaks.com/topic/212337-is-it-possible-to-rewrite-this/#findComment-1106492 Share on other sites More sharing options...
wildteen88 Posted September 2, 2010 Share Posted September 2, 2010 Oh, I never knew that. Learned something new today. Quote Link to comment https://forums.phpfreaks.com/topic/212337-is-it-possible-to-rewrite-this/#findComment-1106497 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.