4

Calling all .htaccess gurus. I need your help!

I'm trying to force a rewrite to include #! in the urls.

So basically I need. http://example.com/biography

To be re-written to http://example.com/#!/biography

If it will make any difference my rewrite rules so far are

  RewriteEngine On
  RewriteCond %{HTTPS} !=on

  RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
  RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

  RewriteCond $1 !^(images|system|files|themes|static|favicon\.ico|robots\.txt|index\.php) [NC]
  RewriteRule ^(.*)$ /index.php/$1 [L]

I suck at this stuff so any help will be greatly appreciated.

Additionally...

I have this test doing what I need it to do in this htaccess tester. http://htaccess.madewithlove.be/ But it won't work when I try it on my site...

RewriteCond %{REQUEST_URI} !^/#!
RewriteRule ^(.*)$ /#!/$1 [L]

No ideas as to why it won't work?

Thanks, Mark.

anubhava
  • 761,203
  • 64
  • 569
  • 643
markstewie
  • 9,237
  • 10
  • 50
  • 72

1 Answers1

8

Have your .htaccess like this:

Options +FollowSymlinks -MultiViews
RewriteEngine on

RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

RewriteRule ^(biography)/?$ /#!/$1 [R,L,NE,NC]

RewriteCond $1 !^(images|system|files|themes|static|favicon\.ico|robots\.txt|index\.php) [NC]
RewriteRule ^(.*)$ /index.php/$1 [L]

Remember you cannot have a condition to check for /#! in .htaccess because that part is handled in browsers only and not sent to web server.

anubhava
  • 761,203
  • 64
  • 569
  • 643
  • 1
    for the record: its primarily the flag `[R]` that does the trick - causing a HTTP 302 Redirect and enabling the browser to handle the anchor (the part after `#` which is handled by the client) – Kaii Apr 02 '18 at 18:41