我在我的ubuntu計算機上有一個測試網站,URL是:mysite.test。我有營銷網頁:http://mysite.test/hello.php,http://mysite.test/goodbye.php等刪除.htaccess中的.php擴展名不起作用
我也有通配符子域:http://user1.mysite.test/page1.php,http://user2.mysite.test/page.1php等
我想所有頁面可訪問不添加「.php」結尾(營銷網站和子域)。我修改了我的.htaccess文件,但它不工作,我不明白爲什麼。 mod_rewrite已啓用並正在運行(下面的「刪除www」規則起作用)。
#/var/www/mysite.test/public_html/.htaccess
RewriteEngine On
#Remove www - this works
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1%{REQUEST_URI} [R=301,QSA,NC]
#Not need .php extension - this does not work
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.php
刪除第一條規則(「刪除www」)不會使其開始工作。
當我去http://mysite.test/hello我得到一個404 Not Found錯誤。去http://mysite.test/hello.php加載就好了。
當我轉到http://user1.mysite.test/page1時,出現404 Not Found錯誤。去http://user1.mysite.test/page1.php加載就好了。
我的虛擬主機文件看起來像這樣:
#/etc/apache2/sites-available/mysite.test.conf
<VirtualHost *:80>
ServerName www.mysite.test
ServerAlias mysite.test
ServerAdmin [email protected]
DocumentRoot /var/www/mysite.test/public_html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
<Directory /var/www/mysite.test>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
</VirtualHost>
<VirtualHost *:80>
ServerName wildcard.mysite.test
ServerAlias *.mysite.test
DocumentRoot /var/www/mysite.test/public_html/app
<Directory /var/www/mysite.test/public_html/app>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
</VirtualHost>
我缺少什麼?謝謝。
會發生什麼,如果有人打你的網站與'/富/酒吧/巴茲'?你會改寫成'foo/bar/baz.php' e目錄存在? –
@ marc-b我在.htaccess文件中添加了''RewriteCond%{REQUEST_FILENAME}!-d'',它仍然不起作用。 –