使用 .htaccess 删除和重定向部分 url

时间:2021-04-12 02:58:27

标签: php regex wordpress .htaccess codeigniter

我正在尝试使用 .htaccess 删除我服务器上的部分 URL。假设我有这个文件夹结构。

根:

  • 官方(codeigniter)
  • 程序(wordpress)
  • .htaccess

/official 是我访问根域 (www.example.com/official) 时的主要文件夹。我想从我的 URL 中删除 /official,而且我已经用这个 .htaccess 脚本做到了这一点。

RewriteEngine On

RewriteBase /

RewriteRule ^$ official/ [L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^((?!official/).+)$ official/$1 [L,NC]

这个脚本的问题是虽然我可以在没有 /official 的情况下访问 ci 项目,但我仍然可以使用 /official 访问它们。我想要的是完全删除 /official 或重定向它,以便 URL 仅显示根域。

我的 CI .htaccess 是这个

RewriteEngine On

RewriteCond %{REQUEST_URI} ^/system.*
RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?/$1 [L]

1 个答案:

答案 0 :(得分:0)

  1. 新建文件夹

  2. 构建另一个 index.php

  3. 将 .htaccess 重定向到新文件夹 (no.1) 内的新 index.php(no.2)

  4. 更改下面htaccess中的文件目录

     # Disable directory browsing
     Options All -Indexes
    
     # ----------------------------------------------------------------------
     # Rewrite engine
     # ----------------------------------------------------------------------
    
     # Turning on the rewrite engine is necessary for the following rules and features.
     # FollowSymLinks must be enabled for this to work.
     <IfModule mod_rewrite.c>
         Options +FollowSymlinks
         RewriteEngine On
    
         # If you installed CodeIgniter in a subfolder, you will need to
         # change the following line to match the subfolder you need.
         # http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewritebase
         # RewriteBase /
    
         # Redirect Trailing Slashes...
         RewriteCond %{REQUEST_FILENAME} !-d
         RewriteCond %{REQUEST_URI} (.+)/$
           RewriteRule ^ %1 [L,R=301]
    
         # Rewrite "www.example.com -> example.com"
         RewriteCond %{HTTPS} !=on
         RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
         RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]
    
         # Checks to see if the user is attempting to access a valid file,
         # such as an image or css document, if this isn't true it sends the
         # request to the front controller, index.php
         RewriteCond %{REQUEST_FILENAME} !-f
         RewriteCond %{REQUEST_FILENAME} !-d
         RewriteRule ^([\s\S]*)$ dir/index.php/$1 [L,NC,QSA]
    
         # Ensure Authorization header is passed along
         RewriteCond %{HTTP:Authorization} .
         RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
     </IfModule>
    
     <IfModule !mod_rewrite.c>
         # If we don't have mod_rewrite installed, all 404's
         # can be sent to index.php, and everything works as normal.
         ErrorDocument 404 index.php
     </IfModule>
    
     # Disable server signature start
         ServerSignature Off
     # Disable server signature end
    
     # Disable directory browsing
     Options All -Indexes
    
  5. index.php

<块引用>
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
require '../codeigniter/public/index.php';
?>
  1. 目录标签(不推荐)
<块引用>
<Directory "/var/www/html">
  AllowOverride AuthConfig
  Order allow,deny
  Allow from all
</Directory>
相关问题