使用.htaccess重定向图像文件夹以外的URL

时间:2018-07-03 01:45:00

标签: php .htaccess mod-rewrite mysqli php-7

我的 public_html 如下所示→

1。 / image / [目录]

2。 / scripts / [目录]

3。 index.php [FILE]

4。 Logo.jpg

还存在其他文件,例如php.ini,error_log 等。

我想从我的数据库表 url_alis 中搜索example.com/ seo-url ,我的代码写在 scripts / get_url_info下。 php 以获取并输出数据

但是其他任何文件

  

ico | gif | jpg | jpeg | png | js | css

图片下的

style (对于css)文件夹下的

我已经尝试了很多,但是以下两个问题仍然存在

  1. 图像和其他文件也将根据此规则进行处理,该规则将返回用于检查 get_url_info.php (例如查询,文件名等)功能的URL文本。 >
  2. 使用 .php 扩展名和 seo-url (apple-mangoes.php)扩展名处理重定向,返回 404 < / strong>

  3. 如果 script / get_url_info.php 中查询的 db→url_alias 表中的mysqli_num_rows == 0,则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]

这里的基本要求是,如果网址仅指向文本(带有(_,-,%,&)等符号,则 REQUEST_URI 请求,是< strong> /正斜杠没有显示内容的文件夹,该文件夹应转到我的 get_url_info.php 脚本,否则通常会处理所有内容。

其他重要的先决条件-

  • 强制HTTPS

  • 强制www

  • 如果找不到无效的URL或内容,则重定向到index.php

1 个答案:

答案 0 :(得分:0)

首先要满足您的先决条件,请使用.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