簡體   English   中英

用 .htaccess 從根文件夾以外的其他文件夾重寫 URL

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

我正在嘗試使用 .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=時,我要做的是從我的“p”目錄中測試測試,這樣我就可以在我的 citati.php 文件中有 $_GET["auto"] 。 我在“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 實現這一點並不重要,我只是想以某種方式做到這一點。

根據您顯示的示例,請確保您的根目錄的 htaccess 規則文件如下。

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

對於您的p目錄/文件夾,該 htaccess 規則文件如下所示。 您需要使用與 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