apache2重写规则:从URL中删除任何path

我在Apache上使用重写规则疯了。

基本上我想实现的是重写任何url,如:

http[s]://www.example.com/something 

 https://www.example.com 

我在apache上有一个VHost,如下所示:

 <VirtualHost *:80> ServerName example.com ServerAlias example DocumentRoot /var/www/html/example_courtesy ServerAdmin [email protected] RedirectMatch 404 /\.git RedirectMatch 404 /\.svn <Directory /> Options FollowSymLinks AllowOverride None </Directory> <Directory /var/www/html/example_courtesy> Options Indexes FollowSymLinks MultiViews AllowOverride None Require all granted DirectoryIndex index.php indice.htm </Directory> RewriteEngine on RewriteCond %{SERVER_PORT} !^443$ RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [L,R] </VirtualHost> <IfModule mod_ssl.c> <VirtualHost *:443> ServerName example.com ServerAlias example DocumentRoot /var/www/html/example_courtesy ServerAdmin [email protected] RedirectMatch 404 /\.git RedirectMatch 404 /\.svn 

[…]

我尝试从第一条规则中删除[L],并将如下所示的重写规则添加到*:443 VirtualHost:

  RewriteEngine on RewriteCond %{SERVER_PORT} !^80$ RewriteRule ^.*$ https://%{SERVER_NAME} [L,R] 

我收到的是一个重写循环,Firefox告诉我“页面没有正确redirect”。

我做了许多重写规则的其他尝试,但没有运气。

我只使用RedirectMatch将https://www.example.com/specific-path这样的特定URL重写为https://www.example.com ,但这不是我一定想要的。

有什么build议么?

我在这里search了一个类似的问题,但我没有find解决我的具体问题。

您的* .443节的RewriteCond有一个明显的问题。 HTTPS(通常)在端口443上运行(如VirtualHostconfiguration所示),但重写条件为“如果服务器端口不是80,则redirect到https:// …”。

所以,打港口443,要求内容,被告知去443,因为你不在80.这是一个循环。 RewriteCond %{SERVER_PORT} !^443$应该可以工作。

就个人而言,我宁愿使用RewriteCond %{HTTPS} !=on