3

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

../index.php?page=details&id=123456

像这样:

../detail/123456

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

4

4 回答 4

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

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

最后

&%{QUERY_STRING} 

确保将其他参数也添加到您的后端脚本中。

于 2012-08-21T14:35:07.470 回答
1

这应该有效:

RewriteEngine On
RewriteRule ^detail/([^/]*)$ /index.php?page=details&id=$1 [L]
于 2012-08-21T14:49:18.627 回答
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. 如果您不想要这个,那么您正在寻找重定向。

于 2012-08-21T14:35:53.803 回答
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>
于 2012-08-21T14:36:35.350 回答