我正在尝试通过htaccess使用参数来实现目录URL



我想在我的网站上显示某些内容。我根据URL参数选择内容,比如page=about。这很好用,我的URL看起来像example.com/mysite/index.php?page=关于。我的问题是,我希望我的URL读example.com/mysite/about,其中最后一部分是页面参数的名称。

我正试图通过htaccess来做到这一点。这是我的确切代码:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?page=$1 [L,QSA]

我在我的页面上看不到任何变化,也看不到URL结构。URL仍在读取。。。index.php?page=关于。我做错什么了吗?我尝试了很多不同的方法,但仍然得到了相同的结果。我也试着简单地这样做,没有任何改变:

RewriteEngine On
RewriteRule ^([a-z]+)/([0-9]+)/?$ index.php?page=$1 [NC]

我觉得它不起作用,所以我尝试重定向到其他网站,它起作用了。因此,htaccess文件正在被读取并能够运行。

任何帮助都会很有帮助!

谢谢,BP

您可以在站点根目录中使用这两条规则。htaccess:

RewriteEngine On
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} s/+(mysite)/index.php?page=([^s&]+) [NC]
RewriteRule ^ /%1/%2? [R=302,L,NE]
# internal forward from pretty URL to actual one
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/mysite/([^/]+)/?$ [NC]
RewriteRule ^ index.php?page=%1 [L,QSA]

最新更新