如何使用.htaccess重写带有两个变量的url

时间:2012-08-21 14:29:29

标签: php .htaccess url-rewriting

我想用.htaccess重写它我有这个网址:

../index.php?page=details&id=123456
像这样:

../detail/123456

但我真的不知道该怎么做。你能帮我改写一下这个网址吗? 或者给我一个简单的例子,我可以理解它是如何工作的

4 个答案:

答案 0 :(得分:6)

RewriteRule ^(.*)/([0-9]+)$ index.php?page=$1&id=$2&%{QUERY_STRING}

第一个(。*)选择任何字符串,例如“细节”。 ([0-9] +)捕获一个数字,在你的情况下为“123456”

最后

&%{QUERY_STRING} 

确保将其他参数添加到您的后端内容中。

答案 1 :(得分:1)

这应该有效:

RewriteEngine On
RewriteRule ^detail/([^/]*)$ /index.php?page=details&id=$1 [L]

答案 2 :(得分:0)

以下是我在网页上使用的一个示例,当它位于子目录中时。

# "learn/degree/index.php?d=mathematics" shows as "learn/degree/mathematics"
RewriteRule ^learn/degree/([a-zA-Z_-]+)$ learn/degree/index.php?d=$1

要完成它,请记得在页面的开头写这个:

RewriteBase /
RewriteEngine On

这是我在页面中使用的'完整'.htaccess,可以给你一些想法。

#Redirect any www. to the page without it. Helps to keep user logged.
RewriteBase /
RewriteEngine On
rewriteCond %{HTTP_HOST} ^www.newfutureuniversity.org [NC]
rewriteRule ^(.*)$ http://newfutureuniversity.org/$1 [R=301,L]

Options +Indexes
Options +FollowSymlinks

# Finally working. Rewrite the user so "/student/Username" will internally be "/student/?user=Username"
RewriteRule ^student/([a-zA-Z0-9_-]+)$ student/index.php?user=$1

# Rewrite the disciplines so "learn/degree/1" will internally be "learn/degree/index.php?dis=1"
RewriteRule ^learn/degree/([1-4])$ learn/degree/index.php?dis=$1

# Rewrite the degrees so "learn/degree/mathematics" will internally be "learn/degree/index.php?d=mathematics"
RewriteRule ^learn/degree/([a-zA-Z_-]+)$ learn/degree/index.php?d=$1

# Rewrite the degrees so "learn/subject/2/5/7" will internally be "learn/subject/index.php?class=2&div=5&section=7"
RewriteRule ^learn/subject/([0-9])$ learn/subject/index.php?class=$1
RewriteRule ^learn/subject/([0-9])/([0-9])$ learn/subject/index.php?class=$1&div=$2
RewriteRule ^learn/subject/([0-9])/([0-9])/([0-9])$ learn/subject/index.php?class=$1&div=$2&section=$3

您必须链接到../detail/123456,然后脚本在内部将其解释为../index.php?page=details&id=123456。如果您不想这样,那么您正在寻找重定向。

答案 3 :(得分:0)

试试这个

<IfModule mod_rewrite.c>
    RewriteEngine on
    Options +FollowSymlinks
    # RewriteBase / add this if necessery, commented intentionally
    RewriteRule ^detail/([0-9]+)$ index.php?page=details&id=$2

</IfModule>