使用htaccess更改URL

时间:2016-03-29 11:49:13

标签: .htaccess

我在文件夹public(root / public)中有一个 .htaccess 文件

我想实现以下转换:

  1. /index.php - > /
  2. index.php?site=siteName - > /siteName/
  3. /siteName//siteName - > serves as ?site=siteName
  4. 到目前为止,这是我完整的 .htaccess 文件(案例3已解决):

    Allow from all
    
    RewriteEngine On
    RewriteRule ^([a-zA-Z0-9]+)$ index.php?current=$1
    RewriteRule ^([a-zA-Z0-9]+)/$ index.php?current=$1
    

1 个答案:

答案 0 :(得分:1)

您可以使用其他规则将旧网址重定向到漂亮的网址并移除index.php

RewriteEngine On

# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} /index\.php\?current=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=302,L,NE]

# remove index.php
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^(.*)index\.php$ /$1 [L,R=302,NC,NE]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([a-zA-Z0-9]+)/?$ index.php?current=$1 [L,QSA]