php - 使用 .htaccess 重定向 URL,不包括图像文件夹
问题描述
我的public_html如下所示 →</p>
1. /图像/ [目录]
2. /scripts/ [目录]
3. index.php [文件]
4. 标志.jpg
其他文件也存在,如 php.ini、error_log等。
我希望从我的数据库表url_alis中搜索example.com/
seo-url ,为此我在scripts/get_url_info.php下编写了代码来获取和输出数据
但任何其他文件,如
ico|gif|jpg|jpeg|png|js|css
不应在此规则下处理图像和样式(用于 css )文件夹下
我已经尝试了很多,但以下任一问题仍然存在
- 图像和其他文件也会根据此规则进行处理,它返回我用来检查get_url_info.php功能的 URL 文本(例如查询、文件名等)
使用.php扩展名后跟seo-url (apple-mangoes.php) 处理重定向,返回404
如果在script/get_url_info.php
mysqli_num_rows == 0
的查询中的db→url_alias表中,URL 应该指向 example.com,而这并没有发生。
我当前的 .htaccess 文件(为不统一的代码道歉)
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]
RewriteRule ^([^?]*) scripts/get_url_info.php?url=$1 [L,QSA] # Process all products
RewriteRule ^/image/([^?]*) /image/$1/$2 [L,QSA]
RewriteRule ^(.*)$ /index.php [NC,L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]
这里的基本要求是,如果 url 仅指向文本(带有 (_,-,%,&) REQUEST_URI请求之类的符号,没有/正斜杠或没有显示内容的文件夹,它应该转到我的get_url_info .php脚本通常会处理所有内容。
其他重要的先决条件 -
强制 HTTPS
强制 www
如果未找到无效 URL 或内容,则重定向到 index.php
解决方案
首先回答您的先决条件,在您的 .HTACCESS 文件中使用以下代码。
强制 HTTPS:
### HTTP to HTTPS ###
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
强制 www:
### Redirect from non-www to www version ###
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{http_host} ^example.com [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,NC]
如果找不到无效的 URL 或内容,则重定向到 index.php:
对于 404 错误,请使用以下代码
### Custom Error Pages ###
Options -Indexes
ErrorDocument 404 /html/404-error.html
当您想将它们重定向到主页时,您可以使用
ErrorDocument 404 /index.php
推荐阅读
- date - 如果我想编写一个序言谓词来比较两个时期(日期)并查看两个时期是否重叠......例如:
- java - 需要在循环中执行 SQL 查询的替代方法
- windows - 如何在 Inno Setup 中执行此操作?
- c# - ASP.NET OAuth 微软单租户登录
- regression - 前向选择和后向消除
- java - 如何使用 LocalDate 作为 TreeTableView / TableView 的列
- java - 当从 Fat-JAR/Uber-JAR 运行代码时,异常 `java.lang.IllegalStateException: zip file closed`
- attributes - xQuery:关联 2 个属性并返回与所述属性相关的数据
- r - Fehler:不能对不存在的列进行子集化。x 列 `plot_id` 不存在
- html - 将背景图像转换为对象问题