使.htaccess文件在子文件夹中工作 [英] Getting .htaccess file to work in subfolders

查看:70
本文介绍了使.htaccess文件在子文件夹中工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我终于想出了如何使用.htaccess文件在我的网站上正常工作。

I finally figured out how to get certain things to work on my website using the .htaccess file.


  1. 重定向所有非www要求使用www版本。完成。

  2. 删除所有php文件扩展名并添加斜杠。完成。

  3. 禁止目录视图。完成。

  4. 限制缓存。完成。

  5. 将404请求重定向到首页。完成。

  1. Redirect all non-www requests to www version. DONE.
  2. Remove all php file extensions and add a trailing slash. DONE.
  3. Prohibit directory views. DONE.
  4. Limit caching. DONE.
  5. Redirect 404 requests to home page. DONE.

这一切似乎都很好,但仅在ROOT目录中。

This all seems to work well, but only in the ROOT directory.

在子文件夹中效果不佳。没有删除PHP扩展。 URL中的文件夹路径消失了。

It doesn't work well in subfolders. PHP extensions aren't removed. Folder paths in URLs disappear.

由于我是.htaccess文件和正则表达式的新手,因此到这一点需要花费一些时间,并且需要反复试验,我我不愿意进一步篡改代码。

As I'm new to .htaccess files and regular expressions, and getting to this point took some time and lots of trial and error, I'm hesitant to tamper with the code any further.

我希望您能得到有关以下方面的指导:

I would appreciate any guidance on:


  1. 如何优化此文件的子文件夹。

  2. 通常如何优化此文件。

谢谢。

RewriteEngine On

# redirect non-www requests to www version
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^example\.net [NC]
RewriteRule ^(.*)$ http://www.example.net/$1 [R=301,L]

RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^example\.net [NC]
RewriteRule ^(.*)$ https://www.example.net/$1 [R=301,L]

# remove .php file extensions
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^/]+)/$ $1.php

# add trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]

# disable directory view on web pages
Options -Indexes

# cached pages will expire in 5 days
ExpiresActive On
ExpiresDefault "access plus 5 days"

# re-direct 404 pages to home page
ErrorDocument 404 /


推荐答案

保持 DocumentRoot / .htaccess 像这样:

# disable directory view on web pages
Options -Indexes

# cached pages will expire in 5 days
ExpiresActive On
ExpiresDefault "access plus 5 days"

# re-direct 404 pages to home page
ErrorDocument 404 /
RewriteEngine On
RewriteBase /

# redirect non-www requests to www (both http and https)
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTPS}s on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=302,L,NE]

# add trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]

## hide .php extension
# To externally redirect /dir/file.php to /dir/file
RewriteCond %{THE_REQUEST} \s/+(.*?/)?(?:index)?(.*?)\.php[\s?] [NC]
RewriteRule ^ /%1%2/ [R=302,L,NE]

# To internally forward /dir/file to /dir/file.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]

一旦确认它可以正常工作,请将 R = 302 替换为 R = 301 。在测试您的mod_rewrite规则时,避免使用 R = 301 (永久重定向)。

Once you verify it is working fine, replace R=302 to R=301. Avoid using R=301 (Permanent Redirect) while testing your mod_rewrite rules.

这篇关于使.htaccess文件在子文件夹中工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆