Apache-2.2

將一個 URL 重定向到同一站點上的另一個技巧?

  • October 11, 2012

我想達到什麼

prod.com 將立即移至 prod.com/cms

當我做永久重定向/ /cms

我有很多 /cmscmscmscmscms 等。有什麼想法嗎?

我的虛擬主機

<VirtualHost *:443>
    ServerName prod.com
    DocumentRoot /home/www/sites/prod/html/development
    <Directory /home/www/sites/prod/html/development>
           AllowOverride All
    </Directory>

    SSLEngine on
    SSLCACertificateFile    /etc/pki/tls/certs/gd_bundle.crt
    SSLCertificateFile    /etc/pki/tls/certs/prod.crt
    SSLCertificateKeyFile /etc/pki/tls/private/prod.key

    <FilesMatch "\.(cgi|shtml|phtml|php)$">
           SSLOptions +StdEnvVars
    </FilesMatch>

    BrowserMatch "MSIE [2-6]" \
    nokeepalive ssl-unclean-shutdown \
    downgrade-1.0 force-response-1.0
    BrowserMatch "MSIE [17-9]" ssl-unclean-shutdown

</VirtualHost>

我的 .htaccess

RewriteEngine On
RewriteCond %{REQUEST_URI} ^/API [NC]
RewriteRule ^(.*)$  /Application/API/api.php [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*)$  /Application/controller.php [L]

DirectoryIndex /Application/controller.php

你得到一個重定向循環的原因是因為Redirect / /cms匹配任何以 / 開頭的路徑並將舊路徑附加到新路徑上。你真正想要的是:

RedirectMatch ^\/$ /cms 

這僅匹配 /

引用自:https://serverfault.com/questions/437445