doushi9729 2019-07-30 16:12
浏览 103

使用https www重定向并使用vhosts,apache2和htaccess尾随斜杠

I have a strange issue with redirecting URLs in an edge case scenario. All of my applications using my framework use https with forced www and a trailing slash on the URLs. The https and forced www redirects are done with virtual hosts (i've included the normal apache2 conf and the certbot https generated ones). The redirection all works apart from a very edge case scenario when one letter is put at the end of the URL with no trailing slash where index.php gets added to the URL, for example:

example.com/aa redirects to example.com/aa/ correctly example.com/a redirects to example.com/index.php/a/ incorrectly

I can't seem to nail down what is causing this issue, but i'm sure its to do with the htaccess file as the 301 redirect (as seen in chrome network) happens before the request hits the code of the website. This happens on both my local and live environments.

Many thanks for all advice!

I've tried using different htaccess configurations with no luck (just made the situation worse).

example.com.conf:

# domain: example.com
# public: /var/www/example/public/

<VirtualHost *:80>
    ServerName example.com
    Redirect permanent / https://www.example.com/
</VirtualHost>

<VirtualHost *:80>
  # Admin email, Server Name (domain name), and any aliases
  ServerAdmin webmaster@example.com
  ServerName  www.example.com
  ServerAlias example.com

  # Index file and Document Root (where the public files are located)
  DirectoryIndex index.html index.php
  DocumentRoot /var/www/example/public

  # Log file locations
  LogLevel warn
  ErrorLog  /var/www/example/log/error.log
  CustomLog /var/www/example/log/access.log combined
RewriteEngine on
RewriteCond %{SERVER_NAME} =www.example.com [OR]
RewriteCond %{SERVER_NAME} =example.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>

example.com-le-ssl.conf:

<IfModule mod_ssl.c>
<VirtualHost *:443>
  # Admin email, Server Name (domain name), and any aliases
  ServerAdmin webmaster@examplw.com
  ServerName  www.example.com
  ServerAlias example.com

  # Index file and Document Root (where the public files are located)
  DirectoryIndex index.html index.php
  DocumentRoot /var/www/example/public

  # Log file locations
  LogLevel warn
  ErrorLog  /var/www/example/log/error.log
  CustomLog /var/www/example/log/access.log combined
Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/www.example.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/www.example.com/privkey.pem
</VirtualHost>
</IfModule>

.htaccess file:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /

    RewriteCond %{REQUEST_URI} /+[^.]+$
    RewriteRule ^(.+[^/])$ %{REQUEST_URI}/ [R=301,L]

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php/$1 [QSA,L]
</IfModule>

Expecting:

example.com/aa to redirect to example.com/aa/ example.com/a to redirect to example.com/a/

Images & files should redirect without the trailing slash.

Requests that already have the trailing slash should not redirect at all.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥30 这是哪个作者做的宝宝起名网站
    • ¥60 版本过低apk如何修改可以兼容新的安卓系统
    • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
    • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
    • ¥50 有数据,怎么用matlab求全要素生产率
    • ¥15 TI的insta-spin例程
    • ¥15 完成下列问题完成下列问题
    • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
    • ¥15 YoloV5 第三方库的版本对照问题
    • ¥15 请完成下列相关问题!