.htaccess mod rewrite - 可变数量的零件

时间:2016-06-13 16:32:22

标签: php .htaccess mod-rewrite url-rewriting get

这是我通过锁定而收集的分离事物的汇编,但我无法弄清楚如何使它们一起工作。

配置

Options +FollowSymLinks
RewriteEngine on

RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
  1. 删除www.

    RewriteCond %{HTTP_HOST} www.sitename.com [NC]
    RewriteRule ^(.*)$ http://sitename.com/$1 [R=301,L]
    
  2. 删除扩展程序

    RewriteCond %{REQUEST_FILENAME}.php -f
    RewriteCond %{REQUEST_URI} !/$
    RewriteRule (.*) $1\.php [L]
    
  3. 在第一个位置管理语言,在第二个位置管理页面。
    示例1:sitename.com/en/newssitename.com/news.php?lang=en读取
    示例2:sitename.com/pt/aboutsitename.com/about.php?lang=pt

    读取
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} -f  [OR]
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule .* - [L]
    RewriteRule ^([^/]+)/([^.]+)  /$2.php?lang=$1 [L,NC,QSA]
    
  4. 管理第一个位置的语言,第二个位置的页面,第三个位置的文章名称,仅当第二个参数为“文章”时
    示例:sitename.com/en/article/article-url-namesitename.com/news.php?urlname=article-url-name&lang=en

    读取
    RewriteCond %{REQUEST_FILENAME} -f  [OR]
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule .* - [L]
    RewriteRule ^([^/]+)/article/(\d+)*$  /news.php?urlname=$2&lang=$1 [L,NC,QSA]
    
  5. 主要问题是3和4.如果另一个不存在,我只能做一个工作。

    最终目标是:

    英语:

    • sitename.com/en
    • sitename.com/en/about
    • sitename.com/en/news
    • sitename.com/en/article/123
    • sitename.com/en/contacts

    葡萄牙语:

    • sitename.com/pt
    • sitename.com/pt/about
    • sitename.com/pt/news
    • sitename.com/pt/article/123
    • sitename.com/pt/contacts

2 个答案:

答案 0 :(得分:1)

用以下内容替换#3和#4块:

RewriteCond %{REQUST_FILENAME} -d [OR]
RewriteCond %{REQUST_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule ^(en|pt)/article/([0-9]+)/?$ /news.php?urlname=$2&lang=$1 [NC,L]
RewriteRule ^([^/]+)/([^/]+)/?$ /$2.php?lang=$1 [NC,L]

答案 1 :(得分:0)

  

sitename.com/en/article/article-url-name从中读取   sitename.com/news.php?urlname=article-url-name&lang=en

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

  

sitename.com/pt/about从sitename.com/about.php?lang=pt读取

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