0

以下の例のように URL を書き換えたいと思います。

http://www.domain.com/index.php  <=>  http://www.domain.com
http://www.domain.com/index.php?lang=de  <=> http://www.domain.com/de
http://www.domain.com/index.php?p1=something  <=>  http://www.domain.com/something
http://www.domain.com/index.php?lang=de&param1=something  <=>  http://www.domain.com/de/something
http://www.domain.com/index.php?p1=something&p2=something-else  <=>  http://www.domain.com/something/something-else
http://www.domain.com/index.php?lang=de&p1=something&p2=something-else  <=>  http://www.domain.com/de/something/something-else

ルールでは、DE だけでなく、他のロケールを追加できるようにする必要があります。また、ユーザーがhttp://domain.com/de/などにアクセスした場合、301 でhttp://domain.com/deにリダイレクトする必要があります(末尾のスラッシュによって URL が重複することはありません)。

助けてください。どうもありがとう。

編集

必要なフォルダーをフィルターで除外しようとしましたが、最初のパラメーターで立ち往生しました

#Rewrite to www
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain.com[nc]
RewriteRule ^(.*)$ http://www.domain.com/$1 [r=301,nc]

#rewrite all physical existing file or folder
RewriteCond %{REQUEST_FILENAME} !-f [OR]
RewriteCond %{REQUEST_FILENAME} !-d

#allow things that are certainly necessary
RewriteCond %{REQUEST_URI} "/images/" [OR]
RewriteCond %{REQUEST_URI} "/media/" [OR]
RewriteCond %{REQUEST_URI} "/script/" [OR]
RewriteCond %{REQUEST_URI} "/style/"

#rewrite rules
RewriteRule .* - [L]
RewriteRule (.*) index.php?lang=$1 [QSA]

#add trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^.*[^/]$ /$0/ [L,R=301]

#Prevent viewing of .htaccess file
<Files .htaccess>
order allow,deny
deny from all
</Files>

#Prevent directory listings
Options All -Indexes
4

1 に答える 1

0

このコードを試すことができます:

Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /

## If the request is for a valid directory
RewriteCond %{REQUEST_FILENAME} -d [OR]
## If the request is for a valid file
RewriteCond %{REQUEST_FILENAME} -f [OR]
## If the request is for a valid link
RewriteCond %{REQUEST_FILENAME} -l
## don't do anything
RewriteRule ^ - [L]

RewriteRule ^(.+?)[^/]$ /$1 [L,R=301]

RewriteRule ^$ /index.php [L]

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

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

RewriteRule ^(\w{3,})/([^/]+)/?$ /index.php?p1=$1&p2=$2 [L,QSA]

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

RewriteRule ^(\w{3,})/?$ /index.php?p1=$1 [L,QSA]    

参考:Apache mod_rewrite の紹介

于 2013-10-04T19:59:20.693 に答える