【问题标题】:.htaccess redirect & hide directory path in url.htaccess 重定向和隐藏 url 中的目录路径
【发布时间】:2013-11-07 05:06:38
【问题描述】:

我在根目录中安装了 wordpress。我想在我的域上使用一些单独的 php 文件作为页面,为此我创建了一个单独的目录,其中包含用于提供 php 文件的所有文件,其目录结构如下:

根文件夹有所有的wordpress文件

我想作为页面的目录

/inc/css/    
/inc/php/    
/inc/img/

PHP 文件中的 CSS 样式表文件目录位置是 ../inc/css 后退一步 & 然后是 css 文件夹。 我想从 URL 中隐藏文件夹,例如从根目录提供文件(从 URL 中隐藏 /inc/php/、/inc/css/ 和 /inc/img/ 文件夹)。 p>

例如:www.domain.com/inc/php/about.php 重定向并重写此 URL 到 www.domain.com/about

我的根目录中的.htaccess

RewriteEngine On
RewriteBase /

# disable directory browsing
Options -Indexes

# Prevent hotlinking of images htaccesstools.com/hotlink-protection
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?domain.com [NC]
RewriteRule \.(jpg|jpeg|png|gif|ico|pdf|flv|jpg|jpeg|mp3|mpg|mp4|mov|wav|wmv|png|gif|swf|css|js)$ - [NC,F,L]

RewriteRule ^login$ http://domain.com/wp-login.php [NC,L]
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

<files wp-config.php>
order allow,deny
deny from all
</files>

<files ~ "^.*\.([Hh][Tt][Aa])">
order allow,deny
deny from all
satisfy all
</files>

我尝试了简单的重定向规则,但文件夹在 URL 中公开。

Redirect 301 /about.php /inc/php/about.php

我在 PHP 文件夹中还有一些文件,我想在这些文件上应用 redirect & rewrite URL & hide folder from URL & remove 的相同规则PHP 扩展。

【问题讨论】:

    标签: .htaccess mod-rewrite


    【解决方案1】:

    www.domain.com/inc/php/about.php 将此 URL 重定向并重写为 www.domain.com/about

    当然,这意味着您不能拥有与 php 文件和例如 css 文件相同的基本文件名。因为如果请求是www.domain.com/about,那么它应该映射到/inc/php/about.php 还是/inc/css/about.css?或者它是一个图像?如果您同时拥有这两个文件,则只有一个会被映射到。

    但如果这确实是您想要的,请尝试在您拥有的盗链规则之后添加这些规则:

    # Externally redirect requests for /inc/
    RewriteCond %{THE_REQUEST} \ /inc/(php|img|css)/([^\?\ ]+)\.(php|css|jpe?g|png|gif) [NC]
    RewriteRule ^ /%2 [L,R=301]
    
    # Check if the request is a php file:
    RewriteCond %{DOCUMENT_ROOT}/inc/php%{REQUEST_URI}.php -f
    RewriteRule ^(.*)$ /inc/php/$1.php [L]
    
    # Check if the request is a css file:
    RewriteCond %{DOCUMENT_ROOT}/inc/css%{REQUEST_URI}.css -f
    RewriteRule ^(.*)$ /inc/css/$1.css [L]
    
    # Check if the request is a jpeg file:
    RewriteCond %{DOCUMENT_ROOT}/inc/img%{REQUEST_URI}.jpg -f
    RewriteRule ^(.*)$ /inc/img/$1.jpg [L]
    
    # Check if the request is a gif file:
    RewriteCond %{DOCUMENT_ROOT}/inc/img%{REQUEST_URI}.gif -f
    RewriteRule ^(.*)$ /inc/img/$1.gif [L]
    
    # Check if the request is a png file:
    RewriteCond %{DOCUMENT_ROOT}/inc/img%{REQUEST_URI}.png -f
    RewriteRule ^(.*)$ /inc/img/$1.png [L]
    

    【讨论】:

    • 嗨@jon Lin 感谢您的回答,上面的代码当然可以工作,并且目录是隐藏的,但 css 不是。这就是我在 about.php 文件 &lt;link rel="stylesheet" href="../css/custom-style.css"&gt; 中链接它的方式
    • 你说只能从/inc/映射一种类型的文件,要么是php要么是css,这就是css没有被应用的原因吗?
    • @SimonKB 如果你的php页面被命名为about.php并且它在一个名为about.css的文件中使用css,那么它们都使用about,因此只能map 到一个或另一个。规则的顺序决定了哪一个。如果这不是问题,那么可能是相对 URL 的问题。
    • 非常感谢伙伴挽救了我的一天,是的,似乎 css 的相对路径是问题所在。所以修好了。你有一个很好的长发男人。
    猜你喜欢
    • 2014-06-17
    • 2023-03-11
    • 1970-01-01
    • 2021-02-27
    • 1970-01-01
    • 2021-01-13
    • 1970-01-01
    • 2012-06-23
    • 1970-01-01
    相关资源
    最近更新 更多