简体   繁体   English

.htaccess查询字符串和重定向

[英].htaccess query string and redirect

I have a .htaccess : 我有一个.htaccess

RewriteEngine On

RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

RewriteCond %{REQUEST_URI}  !(\.css|\.js|\.png)$
RewriteRule ^(.*)$ index.php?v=$1 [NC,L,QSA]

ErrorDocument 404 /src/html/404.php

And in my index.php I get the ' v ' variable and check if the page exist in the database. 在我的index.php中 ,获取“ v ”变量,并检查页面是否存在于数据库中。

If the page exist, I will include the content of the page and if the page doesn't exist, I will include the 404 error page. 如果该页面存在,则将包括该页面的内容;如果该页面不存在,则将包括404错误页面。

Everything work without any problem, except when I try to access an existing directory like : 一切正常,除非我尝试访问现有目录,例如:

example.com/src example.com/src

It will automatically redirect to : 它将自动重定向到:

example.com/src/?v=src example.com/src/?v=src

And show the error page 并显示错误页面

But when I try with : 但是当我尝试:

example.com/src/ (with ' / ' at the end of the URL) example.com/src/(URL末尾带有“ / ”)

It will keep the URL and show the error page ! 它将保留URL并显示错误页面! (This is what I need) (这就是我需要的)

Now I would like to know how can I achieve the same result when the URL end without ' / ' ? 现在,我想知道当URL末尾没有' / '时如何获得相同的结果?

add 2 lines for checking the url is not a really exists file or folder 添加2行以检查url是否确实不存在文件或文件夹

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI}  !(\.css|\.js|\.png)$
RewriteRule ^(.*)$ index.php?v=$1 [NC,L,QSA]

I found a tricky solution, but I don't know if it's a good idea or not ! 我找到了一个棘手的解决方案,但是我不知道这是一个好主意!

I edited my .htaccess file and added : 我编辑了.htaccess文件并添加了:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^.*[^/]$ /$0/ [L,R=301]

This works fine, except it will add a " / " to the end of the URL (It's not a problem, but change the URL which I don't like) 效果很好,除了会在URL的末尾添加一个“ / ”(这不是问题,但是更改了我不喜欢的URL)

Update 更新资料

I found the correct solution : 我找到了正确的解决方案:

DirectorySlash Off

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM