簡體   English   中英

使用 htaccess 清理 URL 重新路由

[英]Clean URL reroute with htaccess

我正在關注本教程,並且有這個文件結構:

└── public
   ├── .htaccess
   └── index.php
└── .htaccess

一切都按預期工作。

.htaccess 如下所示:

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteRule ^$ public/ [L]
    RewriteRule (.*) public/$1 [L]
</IfModule>

我對 .htaccess 了解不多,但我想這會將 URI 重新路由到public目錄。 所以每個請求都會去那里。

公共目錄中的 .htaccess 如下所示:

<IfModule mod_rewrite.c>
    Options -Multiviews
    RewriteEngine On
    RewriteBase /auth/public
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
</IfModule>

上面的一切都按預期工作,但我想增強它,所以它像這樣工作:

  1. URL https://www.something.com/anything保持原樣
  2. 但在服務器上,它的處理方式如下https://www.something.com/index.php?q=anything (技術上是https://www.something.com/public/index.php?q=anything

好的,所以我找到了答案。 我編輯了根 .htaccess 並將最后一行更改為: RewriteRule (.*) public/index.php?s=$1 [L]

整個文件現在看起來像:

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteRule ^$ public/ [L]
    #RewriteRule (.*) public/$1 [L]
    RewriteRule (.*) public/index.php?q=$1 [L]
</IfModule>

將 URI 重新路由到public目錄的重寫規則現在將 URI 重新路由到索引的查詢。

暫無
暫無

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

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