Htaccess rewrite url to hash anchor on homepage

淺唱寂寞╮ 提交于 2019-12-13 19:27:29

问题


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/sales

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


回答1:


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]


来源:https://stackoverflow.com/questions/29165917/htaccess-rewrite-url-to-hash-anchor-on-homepage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!