.htaccess,正确重写同名目录和文件 [英] .htaccess, proper rewriting of directory and file with same name

查看:17
本文介绍了.htaccess,正确重写同名目录和文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到目前为止,我的网站有几个静态页面,其中之一是/portfolio.除其他外,我的 htaccess 隐藏了 .html 扩展名.我想添加一个投资组合目录,但我不想将我现有的投资组合页面作为默认索引文件移动到投资组合目录中.我的/portfolio 页面是我的 Google 附加链接之一,我担心如果它被移动或 url 以某种方式发生变化,Google 会认为它是一个全新的页面.

我的问题是,一旦我添加了/portfolio/目录,每当我尝试访问原始的/portfolio 页面时,都会自动添加一个尾部斜杠并链接到该目录本身.

我尝试了无数种选择,其中一个是将/portfolio/重写为/portfolio,但这会造成无限循环.我也尝试过DirectorySlash Off",但它只在目录内删除了尾部斜杠,它没有恢复对原始/portfolio 页面的访问.

最终,我想保持我的/portfolio 页面原样,链接到目录中的页面,如/portfolio/example,如果/portfolio 或/portfolio/被访问,它将导致显示相同的页面在目录之外,Google 不会认为它是重复的内容.

这里也有类似的问题:.htaccess 将 url 重写为页面或目录 尽管这仍然导致出于某种原因对我来说无限循环,我猜这与隐藏的扩展有关.

这是我的 htaccess-

<前>重写引擎开启# HTML 到 PHPRemoveHandler .html .htm添加类型应用程序/x-httpd-php .htm .html# 隐藏扩展RewriteCond %{REQUEST_FILENAME} !-dRewriteCond %{REQUEST_FILENAME}.html -f重写规则 ^(.*)$ $1.html# 强制 WWWRewriteCond %{HTTP_HOST} ^mydomain.net重写规则 ^(.*)$ http://www.mydomain.net/$1 [R=301,L]# 博客子域RewriteCond %{HTTP_HOST} ^blog.mydomain.net$重写规则 ^(.*)$ http://www.mydomain.net/blog/$1 [R=301,L]

我知道拥有一个与静态页面同名的目录并不是一个好主意,但我真的不想改变现有页面并丢失 Google 附加链接,因此处理这个问题的一种干净而正确的方法将是帮助.

解决方案

这里有两个错误"的地方,有两种解决方法.

首先是apache在应用重写条件之前弄清楚"有一个名为portfolio"的目录.这意味着重写条件接收的是portfolio/"而不是portfolio".

其次,"!-d" 规则是专门避免重写,如果实际上有一个同名的目录

解决方案 1:手动重新路由对投资组合目录的请求以删除斜杠.

# 手动将投资组合/请求重新路由到投资组合RewriteCond %{REQUEST_FILENAME} 投资组合/$重写规则 ^(.*)/$ $1# 隐藏扩展RewriteCond %{REQUEST_FILENAME}.html -f重写规则 ^(.*)$ $1.html

注意删除了!-d"条件.

这样做的缺点是您必须将投资组合"边缘情况直接硬编码到重写规则中,并且仍然会导致浏览器首先被重定向到投资组合/

解决方案2:设置DirectorySlash Off并删除目录存在测试

# 禁用自动目录检测目录斜线# 隐藏扩展RewriteCond %{REQUEST_FILENAME}.html -f重写规则 ^(.*)$ $1.html

设置 DirectorySlash Off 可以最好地解决这个问题,但可能会破坏您网站的其他部分,您实际上需要自动 DirectorySlash.祝你好运,我希望这会有所帮助.

请注意,在测试解决方案 2 时,您的浏览器可能会记住portfolio"到portfolio/"的重定向,并在将请求发送到服务器之前执行重定向.请务必在缓存清晰、干净的环境中进行测试以获得最佳结果.

As of now my website has a few static pages, one of which is /portfolio. Among other things, my htaccess hides the .html extension. I'd like to add a portfolio directory, but I do not want to move my existing portfolio page into the portfolio directory as the default index file. My /portfolio page is one of my Google sitelinks and I am afraid if it is moved or if the url changes in someway, Google will consider it to be a brand new page.

My problem is once I add the /portfolio/ directory, whenever I try to visit the original /portfolio page, a trailing slash is automatically added and it links to the directory itself.

I've tried countless options, one being a rewrite of /portfolio/ to /portfolio, however this creates an infinite loop. I also tried "DirectorySlash Off" but that only removed the trailing slash while being inside the directory, it didn't revert access to the original /portfolio page.

Ultimately, I would like to keep my /portfolio page as-is, linking to pages inside the directory like so /portfolio/example and if either /portfolio or /portfolio/ is accessed it will result in showing the same page which is outside of the directory without Google thinking it is duplicate content.

A similar question exists here: .htaccess rewriting url to page or directory though this still resulted in an infinite loop for me for some reason, I'm guess it has something to do with the hidden extensions.

Here's my htaccess-

RewriteEngine On

# HTML to PHP
RemoveHandler .html .htm
AddType application/x-httpd-php .htm .html

# Hide extension
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*)$ $1.html

# Force WWW
RewriteCond %{HTTP_HOST} ^mydomain.net
RewriteRule ^(.*)$ http://www.mydomain.net/$1 [R=301,L]

# Blog Subdomain
RewriteCond %{HTTP_HOST} ^blog.mydomain.net$
RewriteRule ^(.*)$ http://www.mydomain.net/blog/$1 [R=301,L]

I know it's not a great idea having a directory with the same name as a static page, but I really would rather not alter the existing page and lose the Google sitelink, so a clean and proper way to handle this would be a help.

解决方案

There are two things going "wrong" here, and two ways to fix it.

The first is that apache "figures out" that there is a directory by the name of "portfolio" before the rewrite conditions are applied. That means that the rewrite conditions are receiving "portfolio/" instead of "portfolio".

Second, the "!-d" rule is specifically avoiding the rewrite that you want to make if there is in fact a directory by that name

Solution 1: Manually re-route requests for the portfolio directory to remove the slash.

# Manually re-route portfolio/ requests to portfolio
RewriteCond %{REQUEST_FILENAME} portfolio/$
RewriteRule ^(.*)/$ $1 

# Hide extension
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*)$ $1.html

Note the removal of the "!-d" condition.

The downside to this is that you are having to hard-code the "portfolio" edge case directly into the rewrite rules, and will still result in the browser being first redirected to portfolio/

Solution 2: Set DirectorySlash Off and remove directory exists test

# Disable Automatic Directory detection
DirectorySlash Off

# Hide extension
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*)$ $1.html

Setting DirectorySlash Off would fix this issue the best, but may break other parts of your site where you actually want the auto DirectorySlash. Best of Luck, and I hope this helps.

Note when testing solution 2, your browser may remember the redirect of "portfolio" to "portfolio/" and perform the redirect before it even sends the request to the server. Be sure to test in a cache-clear, clean environment for best results.

这篇关于.htaccess,正确重写同名目录和文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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