Aldo Fernandes Junior
Aldo Fernandes Junior

Reputation: 157

Apache RedirectMatch with Regex

I'm trying to redirect every URL that have no # to the same, but with # after /path/.

Example:

This URL: https://www.example.com/path/test/test

Need redirect to this url: https://www.example.com/path/#/test/test

So I wrote this Redirect Match:

RedirectMatch permanent ^/path/(?!#)(.*)$ /path/#/$2

But it's not working.

Browser shows me this message: ERR_TOO_MANY_REDIRECTS

Upvotes: 4

Views: 3200

Answers (2)

Anthony
Anthony

Reputation: 306

You can try this:

RedirectMatch ^/path/([^#]+)$ /path/#/$1

The captured group ([^#]+) represents any string, minimum 1 character, that doesn't contain #.

Upvotes: 1

anubhava
anubhava

Reputation: 784898

You can just use this rule in your site root .htaccess:

RewriteEngine On

RewriteRule ^/?(path)/(.+)$ /$1/#/$2 [L,NE,NC,R=301]

It will redirect /path/test/test to /path/#test/test.

There is no need to check for presence of # in RewriteRule because client browsers don't send any part after # to web server anyway.

Upvotes: 2

Related Questions