how to rewrite a url with two variables using .hta

2019-04-12 10:45发布

问题:

i want rewrite it with a .htaccess i have this url:

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

like this:

../detail/123456

but I do not really know how to do this. Could you help me to rewrite this url? or give me a simple example that I can understand how it works

回答1:

This should work:

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


回答2:

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

The first (.*) selects any string, e.g. "details". ([0-9]+) catches a number, in your case "123456"

Finally

&%{QUERY_STRING} 

ensures, that additional parameters are added to your backendscript, too.



回答3:

Here is an example I use for my webpage when it's in a subdirectory.

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

To complete it, remember to write at the beginning of the page this:

RewriteBase /
RewriteEngine On

Here's the 'full' .htaccess I use in my page to give you some idea.

#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

You have to make the links to ../detail/123456, then the script interpretes it internally as ../index.php?page=details&id=123456. If you don't want this, then you are looking for a redirect.



回答4:

Try this

<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>