-1

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

btb84
  • 241
  • 1
  • 3
  • 11

2 Answers2

1

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 ([a-z]{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 (-).

scandel
  • 1,692
  • 3
  • 20
  • 39
  • thanks, but doesn't work for me. (does not rewrite any url) – btb84 Jan 15 '16 at 10:44
  • Forgotten to escape the dot. And now ? – scandel Jan 15 '16 at 10:49
  • not rewriting anymore. here's the rewriterule working for php, but unfortunately it rewrites js and css too: ^(.*)/(.*) $2.php?lang=$1 – btb84 Jan 15 '16 at 11:04
  • One thing weird ; usually people want to rewrite clean urls like `mysite.com/it/index` to urls with query strings like `mysite.com/index.php?lang=it`, and you're asking the contrary ? In case that is what you really want, I update my answer. It should work, and not rewrite css. Your rewrite rule is too general ; it takes everything with two parts separated by a slash, so it is normal that it rewrites you js and css files also, if there is a slash in their path. – scandel Jan 15 '16 at 11:21
1

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.

Community
  • 1
  • 1