【问题标题】:.htaccess rewrite engine + redirects.htaccess 重写引擎 + 重定向
【发布时间】:2013-10-05 04:15:41
【问题描述】:

我想重写如下示例中的 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

【问题讨论】:

  • http://www.domain.com/dehttp://www.domain.com/something 不是都遵循相同的模式吗?
  • 可能是的,我只是想区分语言环境和其他参数。
  • 如果它们都相同,那么语言环境和其他参数之间没有区别
  • 要求代码的问题必须表明对所解决问题的最低理解。包括尝试过的解决方案为什么不奏效以及预期结果。
  • 请向我们展示您的代码

标签: php apache .htaccess mod-rewrite redirect


【解决方案1】:

你可以试试这个代码:

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 Introduction

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多