【发布时间】:2021-07-01 12:15:37
【问题描述】:
我正在尝试使用 .htaccess 将 URL 重写为漂亮的 URL。我尝试了很多方法,总是得到或错误 500 或什么都没有发生。我的文件夹树如下所示:
root
index.php
.htaccess
p
.htaccess
citati.php
...
当有人去https://example.com/p/citati/Test-test 将其重写为https://example.com/p/citati?autor=Test-test 时,我要做的是从我的“p”目录中,这样我就可以在我的citati.php 文件中有$_GET["autor"]。我在“p”目录中有很多 .php 文件,所以这也是我在重写时需要担心的。 这是我的根目录下的 .htaccess
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]
RewriteRule ^(.+)p/citati\/(.+)$ p/citati?autor=$1 [NC]
这就是我现在在“p”目录中的 .htaccess 文件:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [L,QSA]
RewriteRule ^p/citati\/(.+)$ p/citati?autor=$1 [NC]
</IfModule>
我是否可以从根 .htaccess 文件或“p”目录 .htaccess 实现这一点并不重要,我只是想以某种方式做到这一点。
【问题讨论】: