Luke
Luke

Reputation: 3046

base or relative path don't work on a .htaccess rewrite rule

With an .htaccess I'm rewriting an url in this way:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

RewriteCond %{QUERY_STRING} !(^|&)lang=[a-z]{2}(&|$) [NC]
RewriteCond %{REQUEST_URI} !^/(en|it) [NC]
RewriteRule ^(?:[a-z]{2}/)?(.*)$ /en/$1 [L,NC,R]

RewriteRule ^([a-z]{2})/(.*)$ /index.php?lang=$1&page=$2 [L,NC,QSA]

to follow this conditions:

http://website.com -> http://website.com/en
http://website.com/en -> http://website.com/en
http://website.com/it -> http://website.com/it
http://website.com/de -> http://website.com/en

See this thread for more info about that.

Now the problem is when I have to access to some assets. For example, I have to load css/styles.css

I tried to change the base location to give an absolute path by adding the base:

<base href="http://website.com/">

or by setting a relative path by putting:

../css/styles.css

The browser ask for the files in the right way:

Request URL:http://website.com/css/styles.css
Status Code:302 Found

But I get no files and the response headers are:

Location:http://website.com/en/css/styles.css

How to solve this problem?

Upvotes: 1

Views: 1893

Answers (1)

Jon Lin
Jon Lin

Reputation: 143896

You need to not redirect your assets. Depending on how you have your site working, you can either exclude requests for certain directories or exclude requests for resources that exist (like a file or directory):

RewriteCond %{QUERY_STRING} !(^|&)lang=[a-z]{2}(&|$) [NC]
RewriteCond %{REQUEST_URI} !^/(en|it) [NC]
RewriteCond %{REQUEST_URI} !^/css
RewriteRule ^(?:[a-z]{2}/)?(.*)$ /en/$1 [L,NC,R]

or

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} !(^|&)lang=[a-z]{2}(&|$) [NC]
RewriteCond %{REQUEST_URI} !^/(en|it) [NC]
RewriteRule ^(?:[a-z]{2}/)?(.*)$ /en/$1 [L,NC,R]

Upvotes: 2

Related Questions