简体   繁体   English

.htaccess,正确重写同名目录和文件

[英].htaccess, proper rewriting of directory and file with same name

As of now my website has a few static pages, one of which is /portfolio. 截至目前,我的网站有一些静态页面,其中一个是/组合。 Among other things, my htaccess hides the .html extension. 除此之外,我的htaccess隐藏了.html扩展名。 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. 我的/投资组合页面是我的Google附加链接之一,我担心如果它被移动或者如果网址发生变化,Google会认为它是一个全新的页面。

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. 我的问题是,一旦我添加了/ portfolio /目录,每当我尝试访问原始/组合页面时,会自动添加一个尾部斜杠并链接到目录本身。

I've tried countless options, one being a rewrite of /portfolio/ to /portfolio, however this creates an infinite loop. 我尝试了无数的选项,一个是/ portfolio / to / portfolio的重写,但是这会产生无限循环。 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. 我也试过“DirectorySlash Off”,但只是在目录内部删除了尾部斜杠,它没有恢复对原始/组合页面的访问。

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. 最后,我想保持我的/组合页面按原样,链接到目录中的页面,如so / portfolio / example,如果访问/ portfolio或/ portfolio /,它将导致显示相同的页面,没有Google认为它是重复内容的目录。

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. 这里存在一个类似的问题: .htaccess重写url到页面或目录虽然由于某种原因这仍然导致我的无限循环,我猜它与隐藏的扩展有关。

Here's my htaccess- 这是我的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. 首先是apache在应用重写条件之前“确定”存在名称为“portfolio”的目录。 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 其次,“! - d”规则专门避免了如果实际上存在该名称的目录则要进行的重写

Solution 1: Manually re-route requests for the portfolio directory to remove the slash. 解决方案1:手动重新路由投资组合目录的请求以删除斜杠。

# 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. 请注意删除“!-d”条件。

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 解决方案2:设置DirectorySlash Off并删除目录存在测试

# 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. 设置DirectorySlash Off会最好地解决此问题,但可能会破坏您实际需要自动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. 请注意,在测试解决方案2时,您的浏览器可能会记住“组合”重定向到“组合/”,并在将请求发送到服务器之前执行重定向。 Be sure to test in a cache-clear, clean environment for best results. 请务必在缓存清晰,干净的环境中进行测试,以获得最佳效果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM