简体   繁体   中英

RewriteRule for only .php files

I need to rewrite this url:

mysyte.com/index.php?lang=it

to:

mysite.com/it/index

without rewriting css href, javascript src and image src. I tried many solutions but none worked, could someone help me?

thanks!

edit:

I tried:

RewriteEngine On
RewriteRule ^(.*)/(.*) $2.php?lang=$1

but this overwrites my css calls too.

then I tried:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)/(.*) $2.php?lang=$1

with the same result

EDIT : In your .htaccess, write the following lines :

# Activate url rewriting
RewriteEngine on
# Rewrite all url of the form /2-letters-language-code/index to index.php/lang=2-letters-language-code
RewriteRule ^([a-z]{2})/index$ /index.php?lang=$1 [QSA,L]

The ([az]{2}) part of the regexp means "every group of two letters", so it will catch "it", but also "fr", "en"... and every other language code in two letters. If this is too general, you can replace it with just (it|en|fr) according to your needs.

And if you need to rewrite not just index to index.php , but whatever alphanumeric string, you can do :

RewriteRule ^([a-z]{2})/([a-zA-Z0-9_]+)$ /$2.php?lang=$1 [QSA,L]

Attention to not be too large in the second parenthesis, otherwise the rule will catch strings you don't want. For exemple, [a-zA-Z0-9_]+ means : every group of 1 or more alphanumeric character and underscores. It excludes slashes (/) and hyphens (-).

This was already answered here : CSS not loading after redirect with htaccess rewrite rule

the easiest way to fix this is to specify the full path to your CSS file in the <head> of the HTML file.

If your mod_rewrite rule is modifying the path to where your CSS file is, you'd also want to place this before your RewriteRule :

 RewriteCond %{REQUEST_FILENAME} !-f 

This makes sure that the request doesn't match a file before rewriting it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM