简体   繁体   English

用 .htaccess 从根文件夹以外的其他文件夹重写 URL

[英]Rewriting URL from other than root folder with .htaccess

I am trying to make URL rewrite to nice looking URL with .htaccess.我正在尝试使用 .htaccess 将 URL 重写为漂亮的 URL。 I was trying so many ways and always get or error 500 or nothing is happening.我尝试了很多方法,总是得到或错误 500 或什么都没有发生。 My folder tree looks like this:我的文件夹树如下所示:

 root
    index.php
    .htaccess
    p
        .htaccess
        citati.php 
        ...

What I am trying to do is from my "p" directory when someone goes to https://example.com/p/citati/Test-test to rewrite that to https://example.com/p/citati?autor=Test-test so I can have $_GET["autor"] in my citati.php file.当有人转到https://example.com/p/citati/Test-test将其重写为https://example.com/p/citati?autor=时,我要做的是从我的“p”目录中测试测试,这样我就可以在我的 citati.php 文件中有 $_GET["auto"] 。 I have many.php files in "p" directory so that is also what I need to worry about when rewriting.我在“p”目录中有很多.php 文件,所以这也是我在重写时需要担心的。 This is my .htaccess from root这是我的 .htaccess 从根

RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]
RewriteRule ^(.+)p/citati\/(.+)$ p/citati?autor=$1 [NC]

And this is what I have now in my "p" directory .htaccess file:这就是我现在在“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>

It doesn't matter if I can achieve this from root .htaccess file or from "p" directory .htaccess I just want somehow to do this.我是否可以从根 .htaccess 文件或“p”目录 .htaccess 实现这一点并不重要,我只是想以某种方式做到这一点。

As per your shown samples, please do have your root's htaccess Rule file as follows.根据您显示的示例,请确保您的根目录的 htaccess 规则文件如下。

RewriteEngine ON
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]

For your p directory/folder have that htaccess Rule file as follows.对于您的p目录/文件夹,该 htaccess 规则文件如下所示。 You need to use same rules from root htaccess, you could inherit it.您需要使用与 root htaccess 相同的规则,您可以继承它。

RewriteOptions InheritBefore
RewriteEngine ON
RewriteBase /p/

RewriteCond %{THE_REQUEST} \s/(citati[^.]*)\.php\s [NC]
RewriteRule ^ %1? [R=301,L]

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM