使用.htaccess

时间:2017-04-05 17:59:13

标签: .htaccess url-rewriting

我有一个动态创建的网址page.php?id=param

在此网址中,page.phpparam是动态的,因此我的HTML链接看起来像

<a href="page.php?id=param">link</a>

我需要此网址为

mydomain.com/param

它应该转到page.php,参数id应该来自page.php

这是我现在正在使用的代码

# To externally redirect /dir/foo.php?id=123 to /dir/foo/123
RewriteCond %{THE_REQUEST} ^GET\s([^.]+)\.php\?id=([^&\s]+) [NC]
RewriteRule ^ %1/%2? [R,L]

# To internally forward /dir/foo/12 to /dir/foo.php?id=12

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

但重写如下:

mydomain.com/page/param

所需的输出应为:

mydomain.com/param

1 个答案:

答案 0 :(得分:0)

RewriteEngine On
RewriteRule    ^page/([A-Za-z0-9-]+)/?$    page.php?id=$1    [NC,L] 

L(最后 - 停止处理规则)
NC(不区分大小写)

另请参阅本教程URL Rewriting for Beginners