.htaccess用于语言检测,重定向和纯净网址 [英] .htaccess for language detection, redirecting + clean urls
问题描述
我对.htaccess不太熟悉,但是我设法将以下文件放在一起.遗憾的是,它无法正常工作.
I'm not very familiar with .htaccess, but i managed to put the following file together. Sadly it's not working..
它必须做什么: -检测用户是否为法语=重定向至example.com/fr -检测用户是否使用其他语言=重定向到example.com/nl -显示所有不含.html和.php的网址
What it had to do: - Detect if a user is french = redirect to example.com/fr - Detect if user is any other language = redirect to example.com/nl - Show all urls without .html and .php
这是我用于语言检测的代码,但是会循环.
This is the code i have for the language detection, but it loops..
RewriteEngine on
RewriteCond %{HTTP:Accept-Language} (fr) [NC]
RewriteRule .* http://www.example.com/fr [R,L]
RewriteRule .* http://www.example.com/nl [R,L]
这是我拥有的纯净网址的代码,但仅适用于.html,而且我同时需要.html和.php
And this is the code i have for the clean urls, but it's only for .html, and i need both for .html and .php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+)$ $1.html [L,QSA]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*\.html\ HTTP/
RewriteRule ^(.*)\.html$ /$1 [R=301,L]
那么我该如何使这两个文件一起工作并放在同一个文件中?问候.
So how do i get both of these working, and put together in the same file? Greets.
推荐答案
您需要检查您是否尚未在/fr/
或/nl/
目录中:
You need to check that you're not already in the /fr/
or /nl/
directories:
RewriteEngine on
RewriteCond %{HTTP:Accept-Language} (fr) [NC]
RewriteRule ^(?!fr/)(.*)$ http://www.example.com/fr/$1 [R,L]
RewriteRule ^(?!nl/)(.*)$ http://www.example.com/nl/$1 [R,L]
然后,您只需要复制html和php的规则:
Then you simply need to duplicate your rules for both html and php:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}.php -f
RewriteRule ^(.+)$ $1.php [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}.html -f
RewriteRule ^(.+)$ $1.html [L,QSA]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*\.(php|html)($|\?|\ )
RewriteRule ^(.*)\.(php|html)$ /$1 [R=301,L]
这篇关于.htaccess用于语言检测,重定向和纯净网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!