apache - .htaccess:将根 url 重定向到子目录,但保留根 url

标签 apache .htaccess mod-rewrite

我正在清理我的域的目录结构(在根目录中设置根 url 的内容对我来说是不幸的!),并且需要一些关于如何正确使用 RewriteRule 的见解。

要点

我希望domain.tld使用domain.tld/subdirectory/,但在网址中仍显示为domain.tld

到目前为止我的 .htaccess

Options +FollowSymlinks
RewriteEngine On

#Force removal of wwww subdomain
RewriteBase /
RewriteCond %{HTTP_HOST} ^www.domain.tld
RewriteRule ^(.*)$ http://domain.tld/$1 [R=301,L]

#Trailing slash
RewriteRule ^/*(.+/)?([^.]*[^/])$ http://%{HTTP_HOST}/$1$2/ [L,R=301]

#Redirect root url to subdirectory
RedirectMatch 301 ^/subdirectory/$ http://domain.tld/
RewriteRule ^/$ /subdirectory/?$ [L]

RedirectMatch 效果很好。不幸的是,最后一个 RewriteRule 似乎应该足够简单,但事实并非如此。根目录中的旧内容设置仍然出现。

我在这里缺少什么?

更新:已解决

简单的修复,我对 .htaccess/apache 的经验不足,无法解释原因。

我有:

RewriteRule ^/$ /subdirectory/?$ [L]

删除一个斜杠修复了所有问题:

RewriteRule ^$ /subdirectory/?$ [L]

现在我的问题是:这是为什么?

最佳答案

试试这个:

RewriteEngine on
RewriteBase /

RewriteCond %{REQUEST_URI} !^/subdirectory/

# REWRITES ALL URLS 
# [REPLACE "domain" WITH THE ACTUAL DOMAIN, 
# WITHOUT THE TLD (.com, .net, .biz, etc)]
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.

# REWRITE ALL THOSE TO subdirectory
RewriteRule ^(.*)$ /subdirectory/$1 [L]

BR 斯皮罗斯

关于apache - .htaccess:将根 url 重定向到子目录,但保留根 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6774421/

相关文章:

apache - 重写所有带有 .html 扩展名的 url(index.html 除外)

java - NetBeans 中的 Apache Ivy 用于解析导入

php - htaccess url 重写许多链接并保护 css 和 js 文件

apache - Hadoop数据节点未运行

php - 在动态 PHP 网站中实现 Mod Rewrite

apache - 在一个 ip 地址下在 apache 上托管多个网站

php - htaccess - 如果 POST 数据不重写 URL

linux - 将域指向托管服务器文件夹时出现问题

python - 如何配置 Apache 2 以使用 %x{} 从 Ruby CGI 应用程序运行 Python 脚本

git init 权限被拒绝-/private/etc/apache2