簡體   English   中英

搜索引擎優化 URL - .htaccess

[英]Seo friendly URL - .htaccess

在我的主機的根文件夾中,我有一個 htaccess 文件,其中包含以下代碼,這些代碼重定向到 https 和 www.:

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

在 /news/ 文件夾中,我有 entry.php 文件,例如?slug=this-is-first-entry。 我希望它看起來像這樣https://www.example.com/news/this-is-first-entry

所以我想把這個https://www.example.com/news/entry.php?slug=this-is-first-entry重定向到這個https://www.example.com/news/this-is-first-entry

我在 /news/ 文件夾中的 .htaccess 中有此代碼:

RewriteRule ^([^/\.]+)$ entry.php?slug=$1 [NC,L]

它工作正常,但從根文件夾重定向 https 和 www 不起作用。 請幫忙,我不熟悉htaccess。

這是一個拇指規則。 如果當前目錄有一個 .htaccess 並且RewriteEngine ON那么它會覆蓋父 .htaccess 指令。 如果您希望先應用父 .htaccess 規則,請使用以下選項:

RewriteOptions InheritBefore

RewriteEngine On線之前。

因此 /news/ 文件夾中的 htaccess 文件將變為:

RewriteOptions InheritBefore
RewriteEngine ON
RewriteRule ^([^/\.]+)$ entry.php?slug=$1 [NC,L]

附加建議:如果您嘗試重寫不存在的文件和目錄,如果是這種情況,則將 htaccess 放在您的新聞文件夾中,如下所示。

RewriteOptions InheritBefore
RewriteEngine ON
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/\.]+)$ entry.php?slug=$1 [NC,L]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM