Amxx
Amxx

Reputation: 3070

htaccess rewrite to add fragment

I have an index page index.php with shows the right section depending on the fragment:

Are valid:

/index.php
/index.php#About
/index.php#Contact
/index.php#404error
...

by defining DirectoryIndex index.php I made sure request like /#About are understood weel, but still I'd like to rewrite url to manage nice error display

I tried doing

ErrorDocument 404 /index.php#404error

but that is not good the the url not being rewritten makes that the webpage doesn't have access to the fragment ...

I also tried using RewriteRule

ErrorDocument 404 /404
RewriteEngine on
RewriteRule /404 /index.php#404 [R=301,L]

How can I achieve such a behaviour ?

Upvotes: 1

Views: 201

Answers (1)

anubhava
anubhava

Reputation: 786329

To get fragment in browser you will have to use full redirect like this:

ErrorDocument 404 http://example.com/index.php#404error

EDIT: To keep protocol same use this rule:

RewriteEngine On

# If the request is not for a valid directory
RewriteCond %{REQUEST_FILENAME} !-d
# If the request is not for a valid file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ /index.php#404error [L,NE,R=302]

Upvotes: 1

Related Questions