htaccess将网址重写为首页上的哈希锚 [英] Htaccess rewrite url to hash anchor on homepage
问题描述
我需要为我在Apache Web服务器的codeigniter中内置的网站创建htaccess规则.
我基本上有一个单页网站,可以在首页上显示该网站的各个部分.
例如...
http://www.example.com/sales >
http://www.example.com/portfolio >
...所有内容均显示为首页上的部分,因此仅显示在一页上
我的计划是使用锚标记和哈希将用户定向到首页上的正确区域,但是我无法弄清楚正确的htaccess规则来修改网址以重定向到首页和正确的锚点
因此,我更喜欢这样做的方法是像这样将规则重定向到首页...
**http://www.example.com/sales**
重定向到
**http://www.example.com/#sales**
如果该网址有更多目录,我需要它们进入首页上的最高父目录
例如...
**http://www.example.com/sales/future/page1**
重定向到
**http://www.example.com/#sales**
我还需要确保我隐藏了显示在该段开头的index.php!
任何能够使这项工作奏效的想法或替代建议都将得到广泛应用
亲切的问候
您可以在DOCUMENT_ROOT/.htaccess
文件中使用以下代码:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)(/|$) /#$1 [L,NE,R=302]
I need to create an htaccess rule for a website i built in codeigniter for an apache web server.
I basically have a one page website which displays sections of the site on the the homepage.
for example...
http://www.example.com/portfolio
...all appear as sections on the homepage so it display on one page only
My plan is to use anchor tags and hashes to direct the user to the right area on the homepage but I can't figure out the correct htaccess rule to modify the url to redirect to the homepage and to the correct anchor point
So my prefered method to do this would be to a rule to redirects to the homepage like so...
**http://www.example.com/sales**
redirects to
**http://www.example.com/#sales**
And if the url has more directories I need them to go to the highest parent on the home page
for example...
**http://www.example.com/sales/future/page1**
redirects to
**http://www.example.com/#sales**
I would also need to make sure that i hide the index.php that displays at the beginning of the segment!
Any ideas or alternative suggestion to get this working would be greatly appriciated
Kind Regards
You can use this code in your DOCUMENT_ROOT/.htaccess
file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)(/|$) /#$1 [L,NE,R=302]
这篇关于htaccess将网址重写为首页上的哈希锚的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!