[英]Simple mod_rewrite issue
我有一個很奇怪的問題
我正在玩.htaccess,並嘗試將所有請求重定向到/ test /文件夾的索引文件。
我的站點位於本地htdocs文件夾中的/ test /文件夾中。 當前沒有其他文件。
我的期望:當我訪問任何URL時(例如/test/category/one/
),我應該重定向到/test/index.php
我收到404 Not Found
怎么辦
我的.htaccess看起來像這樣:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /test/index.php?__route=$1 [L,QSA]
我試過設置RewriteBase /test/
這是很簡單的,為什么它不起作用?
我在另一個文件夾中有一個Wordpress網站,並且可以完美地與自定義重寫一起使用。
我什至將Wordpress的.htaccess內容復制到測試站點,用/ test /代替了重寫基礎和最后一條規則。
Wordpress的.htaccess :(適用於在同一服務器上的單獨WP安裝)
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /test/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /test/index.php [L]
</IfModule>
# END WordPress
我已經為此苦苦掙扎了一段時間,在沒有幫助的情況下閱讀了很多SO文章。
我什至寫了一個重寫日志文件,但是當我瀏覽到測試站點時卻什么也沒顯示,但是訪問Wordpress站點會寫很多行。
我在Win64機器上運行XAMPP。
任何幫助將不勝感激! =)
更新 :另外,請確保正確設置.htaccess文件中的行結尾。 Apache有時可以阻塞不包含換行符(\\ n)的任何內容。
所以在我看來,您想要(在某種程度上)模仿WordPress在做什么。 當我開發一些做同樣事情的軟件時,這就是我處理這種情況的方式:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /test
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [L]
RewriteRule ^.*$ /index.php [L]
</IfModule>
對於存在的文件(即-f
或-d
測試通過),我們將為它們提供不變的服務。 否則,我們會將傳入的請求重定向到index.php。 請注意,路徑的/test
部分未包含在RewriteRule中,因為RewriteBase設置了我們的起始位置。 因此,在您的示例中,我認為最終將是:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /test
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [L]
RewriteRule ^(.*)$ /index.php?__route=$1 [L,QSA]
</IfModule>
FWIW,我不是.htaccess專家。 過去,我只是覺得這對我有用。
另外,如果您使用共享主機(例如DreamHost),則可能需要設置適當的規則以允許使用默認錯誤文檔。 一些共享的Web主機會為錯誤情況提供單個文件(failed_auth.html是一個示例)。 如果您沒有過濾掉這種情況,則可能會以404結尾。
這應該做的伎倆:
# Activate the rewrite module.
RewriteEngine On
# Ensure the requested URL is not a file.
RewriteCond %{REQUEST_FILENAME} !-f
# Ensure the requested URL is not a directory.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?__route=$1 [L,QSA]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.