sparecycle
sparecycle

Reputation: 2058

.htacces: RewriteMatch all traffic EXCEPT for two URLs

I'm currently using the below method to redirect users on my site from the root directory to a subdirectory as followed:

RewriteEngine on

############################################
## Forcing https

RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://website.com/$1 [R,L]

############################################
## Capturing all traffic and redirecting it to necessary subdirectory

RedirectMatch ^/$ /subdirectory/

This works well and I can still get to one of my exceptions which is /admin (Full URL: /index.php/admin) no problem. I didn't have to specify any exception - it just worked.

Now I am trying to access my API URL and it manages to redirect it to /subdirectory instead of allowing it. The URL: http://website.com/index.php/api/action

I see there is a way of excluding directories with the RewriteRule method but does this apply for the RedirectMatch method as well?

RewriteEngine on
RewriteRule !^uploads($|/) http://example.com%{REQUEST_URI} [L,R=301]

Upvotes: 1

Views: 1228

Answers (1)

anubhava
anubhava

Reputation: 785266

Better to stick with mod_rewrite instead of using mod_alias based rules here:

RewriteEngine on

############################################
## Forcing https

RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://website.com/$1 [R,L]

############################################
## Capturing all traffic and redirecting it to necessary subdirectory
RewriteCond %{THE_REQUEST} !\s/+(index\.php/)?(api|admin) [NC]
RewriteRule ^/?$ /subdirectory/ [L,R]

Upvotes: 1

Related Questions