Php 使用htaccess重写url

Php 使用htaccess重写url,php,html,.htaccess,redirect,url-rewriting,Php,Html,.htaccess,Redirect,Url Rewriting,我想重写我的网址 这是我的index.html页面 <a href="http://localhost/htac/index.php?url=main&page=1">Main</a> <br> <a href="http://localhost/htac/index.php?url=about&page=2">About</a> <br> <a href="http://localhost/htac/

我想重写我的网址 这是我的index.html页面

<a href="http://localhost/htac/index.php?url=main&page=1">Main</a> <br>
<a href="http://localhost/htac/index.php?url=about&page=2">About</a> <br>
<a href="http://localhost/htac/index.php?url=contact&page=3">Contact</a> <br>
我想把我的网址改成

http://localhost/htac/index/about/2 (http://localhost/htac/index/{url}/{page})
但url是:

http://localhost/htac/index.php?url=about&page=2

让您的
/htac/.htaccess
如下:

RewriteEngine on
RewriteBase /htac/

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

RewriteCond %{REQUEST_FILENAME} !-f    
RewriteCond %{REQUEST_FILENAME} !-d    
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?url=$1&page=$2 [L,QSA]

您需要将HTML的源代码更改为以下链接:

,您是否尝试过
http://localhost/htac/index/about/2
查看是否解决?否,未使用此链接未找到此服务器上未找到请求的URL/htac/index/about/2。IMO
/index/
也应避免使用此URL。这就是为什么我写了一个关于漂亮URL应该是什么的注释。我想用htacces@Beginner将此代码添加到htaccess这是用于htaccess@Beginner然后从使用有效的URL开始!anubhava的答案是正确的。@anubhava ps。我还要添加
NC
参数。
RewriteEngine on
RewriteBase /htac/

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

RewriteCond %{REQUEST_FILENAME} !-f    
RewriteCond %{REQUEST_FILENAME} !-d    
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?url=$1&page=$2 [L,QSA]