Jump to content

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/212337-is-it-possible-to-rewrite-this/
Share on other sites

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

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

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]

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.