2016-02-26 16 views
2

我必須設置的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?如果不是,我錯過了什麼?

謝謝!

編輯: .htaccessmy-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] 
+0

不,它不是目錄,.htaccess位於域的根目錄中。在'my-test-script.php'的相同文件夾中。 – Fasoeu

+0

Apache/2.4.7。 DocumentRoot是'/ var/www/html/client/project_name/domain/subdomain /' – Fasoeu

+0

是的,它們都在'/ var/www/html/client/project_name/domain/subdomain /' – Fasoeu

回答

0
RewriteEngine On 
RewriteBase/

RewriteRule ^/test/?$ /my-test-script.php [NC,L,QSA] 

改爲嘗試。這對我來說是適用於我的測試環境。

+0

它不工作: '未找到 所請求的URL /測試不會在這個server.' – Fasoeu

1

您的my-test-script.php與您的.htaccess位於同一水平嗎?如果是,請執行以下操作:

RewriteEngine On 
RewriteBase/

RewriteRule ^test/?$ my-test-script.php [NC,L,QSA] 
+0

的.htaccess發現我的測試,script.php的是在同一個文件夾中(我將編輯我的問題)。可悲的是你的解決方案不起作用。 – Fasoeu

+0

http://example.com/test(可悲的是我目前不能共享一個真實的公共網址,但它具有完全相同的結構 - 協議,域,路徑) – Fasoeu

+0

它是一個子域嗎? –