[英]Mod rewrite without .htaccess
假設我有一個名為
foo.com
我可以訪問foo.com
,它運行在根文件夾中找到的index.php
。 我的問題是:如何在不啟用htaccess的情況下編輯虛擬主機文件以啟用重寫mod?
我的目標是能夠寫
http://foo.com/bar/loremipsum/dolor
進入我的瀏覽器地址欄,並運行index.php,而不管URL中的/字符數如何。 我的index.php將處理用/分隔的參數
我該如何實現?
編輯:虛擬主機文件:
<VirtualHost *:80>
ServerName myproject.com
ServerAlias www.myproject.com
ServerAdmin webmaster@localhost
DocumentRoot /opt/apps/myproject
<Directory /opt/apps/myproject>
# disable htaccess
AllowOverride None
# route everything to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ /index.php [L]
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
編輯:問題,正如公認的答案所暗示的是,該主機不包含打開重寫引擎的行。 該行出現在答案中。 解決了問題。
要禁止使用htaccess,您需要在<Directory>
容器中將此指令用於文檔根目錄,然后可以將mod_rewrite規則放在同一容器中:
<Directory "/var/www/htdocs/">
# disable htaccess
AllowOverride None
# route everything to index.php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ /index.php [L]
</Directory>
假設“ / var / www / htdocs”是您的文檔根目錄。
為了確保已加載mod_rewrite,請檢查httpd.conf文件中是否包含包含mod_rewrite的LoadModule
行,並確保未對其進行注釋。 更改vhost配置后,您將需要重新啟動服務器。
簡短說明:
這些行:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
是檢查請求是否不是現有文件( -f
)或現有目錄( -d
)的條件。 這些條件有兩個主要目的:
index.php
也不會被重寫。 由於index.php
是現有文件,因此條件將停止重寫引擎。 如果您希望所有內容(包括圖像或其他內容) 都路由到index.php
,則可以將2個條件更改為:
RewriteCond %{REQUEST_URI} !^/index.php
這樣,除index.php
之外的所有內容都將被重寫。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.