的.htaccess,目录的正确重写,并具有相同名称的文件重写、正确、名称、文件

2023-09-02 00:21:28 作者:输过败过゛但不曾怕过°

截至目前我的网站有几个静态页面,其中之一就是/组合。别的不说,我的htaccess隐藏.html扩展名。我想补充一个投资组合的目录,但我不希望我的移动现有的投资组合页面到投资组合的目录默认索引文件。我/组合页面是我的谷歌网站链接之一,我很害怕,如果它被移动或以某种方式链接的变化,谷歌会认为这是一个全新的页面。

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.

我已经试过无数的选择,一个是的/组合重写/到/组合,但是这将创建一个无限循环。我也尝试过DirectorySlash关,但只去掉了后面的斜杠,而作为目录中,并没有恢复访问原始/投资组合页面。

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.

有一个类似的问题存在的位置: http://stackoverflow.com/questions/1237531/htaccess-rewriting-url-to-page-or-directory尽管这仍然导致了一个无限循环,我出于某种原因,我猜想它是与隐藏的扩展名。

A similar question exists here: http://stackoverflow.com/questions/1237531/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.

下面是我的htaccess的 -

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".

二,!-d规则是专门避免了你想,如果有,其实是一个目录中的名称

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

解决方法1:手动重新发送请求的组合目录删除斜线

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

请注意摘取了!-d状态。

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

这样做的缺点是,你不必直接硬code中的组合边缘的情况下进入重写规则,仍然会导致浏览器被第一重定向到投资组合/

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/

解决方法2:设置DirectorySlash关闭并删除目录存在测试

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

设置DirectorySlash关将解决这个问题最好的,但可能会破坏你的网站的其他部分,你真正想要的汽车DirectorySlash。最好的运气,我希望这会有所帮助。

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.

请注意,当测试解决方案2,您的浏览器可能还记得投资组合的重定向到组合/和执行重定向之前就发送请求到服务器。一定要测试在缓存清晰,干净的环境,最好的结果。

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.