我必須設置的URL http://subdomain.example.com/test
,使其能夠調用真正的文件/my-test-script.php
。問題的.htaccess重定向和重寫規則(有或無的RewriteCond)
我不明白,爲什麼下面的代碼工作:
RewriteEngine On
RewriteBase/
RewriteCond %{REQUEST_URI} ^\/?test\/?$
RewriteRule ^\/?(.*)$ /my-test-script.php [NC,L,QSA]
,而這一個不工作:
RewriteEngine On
RewriteBase/
RewriteRule ^\/?test\/?$ /my-test-script.php [NC,L,QSA]
它是強制性的RewriteCond?如果不是,我錯過了什麼?
謝謝!
編輯: .htaccess
和my-test-script.php
都處於根/。
編輯2: 搞笑更新:我看到它的工作原理與
RewriteRule test /my-test-script.php [NC,L,QSA]
但與
RewriteRule ^test$ /my-test-script.php [NC,L,QSA]
編輯3: 我創建了一個腳本,顯示了$ _GET參數:
RewriteCond %{REQUEST_URI} !^\/?show\-parameters\.php$
RewriteRule ^(.*)$ /show\-parameters.php?path=$1 [NC,L,QSA]
,當我打電話http://subdomain.example.com/test
我看到:
Array ([path] => /var/www/html/client/project_name/domain/subdomain/test)
編輯4: 這裏我的虛擬主機:
<VirtualHost *>
ServerAdmin [email protected]
DocumentRoot /var/www/html
RewriteEngine On
### HTTP: from www to non-www ###
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1$1 [R=301,L]
<Directory /var/www/html>
Options -Indexes +FollowSymLinks +MultiViews
AllowOverride All
Order allow,deny
allow from all
RewriteBase/
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
<VirtualHost *:80>
ServerAdmin [email protected]
ServerName subdomain.example.com
ServerAlias www.subdomain.example.com
DocumentRoot /var/www/html/client/project_name/domain/subdomain
RewriteEngine On
### HTTP: from www to non-www ###
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1$1 [R=301,L]
<Directory /var/www/html/client/project_name/domain/subdomain>
Options -Indexes +FollowSymLinks +Multiviews
AllowOverride All
Order allow,deny
allow from all
RewriteBase/
</Directory>
</VirtualHost>
[解決]編輯5: 我只是去掉了RewriteBase /
線從.htaccess和以下規則現在可以使用:
RewriteEngine On
RewriteRule ^\/?test\/?$ /my-test-script.php [NC,L,QSA]
不,它不是目錄,.htaccess位於域的根目錄中。在'my-test-script.php'的相同文件夾中。 – Fasoeu
Apache/2.4.7。 DocumentRoot是'/ var/www/html/client/project_name/domain/subdomain /' – Fasoeu
是的,它們都在'/ var/www/html/client/project_name/domain/subdomain /' – Fasoeu