如何使用2个传递参数来更改URL与htaccess?

时间:2014-02-16 12:47:40

标签: php regex apache .htaccess mod-rewrite

说我是否有一个看起来像的网址:

localhost/category.php?id=name&phrase=something

我可以使用什么.htaccess重写规则代码使其看起来像:

localhost/name/something 

以下是我的.htaccess文件目前的样子:

Options +FollowSymLinks
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/?article/([^/]*)$ /article.php?id=$1 [L]
RewriteRule ^/?wrestler/([^/]*)$ /wrestler.php?id=$1 [L]

# Removes the .php extension from pages
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) $1.php [L]

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

您可以使用此规则:

Options +FollowSymLinks
RewriteEngine On
RewriteBase /

# external redirect from /category.php?id=name&phrase=something to /name/something
RewriteCond %{THE_REQUEST} \s/+category\.php\?id=([^&]+)&phrase=([^\s&]+) [NC]
RewriteRule ^ %1/%2? [R=302,L]

# skip files and directories from rewrites
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]

RewriteRule ^/?article/([^/]+)/?$ article.php?id=$1 [L,QSA]

RewriteRule ^/?wrestler/([^/]+)/?$ wrestler.php?id=$1 [L,QSA]

# Removes the .php extension from pages
RewriteCond %{DOCUMENT_ROOT}/$1.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]

RewriteRule ^([^/]+)/([^/]+)/?$ category.php?id=$1&phrase=$2 [L,QSA]