首页 > 解决方案 > 使用 .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 )文件

我已经尝试了很多,但以下任一问题仍然存在

  1. 图像和其他文件也会根据此规则进行处理,它返回我用来检查get_url_info.php功能的 URL 文本(例如查询、文件名等)
  2. 使用.php扩展名后跟seo-url (apple-mangoes.php) 处理重定向,返回404

  3. 如果在script/get_url_info.phpmysqli_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脚本通常会处理所有内容。

其他重要的先决条件 -

标签: php.htaccessmod-rewritemysqliphp-7

解决方案


首先回答您的先决条件,在您的 .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

推荐阅读