DeathKing
DeathKing

Reputation: 311

.htaccess redirect if certain parameters are met

To begin, I would like to say i barely have any knowledge with the .htaccess file.

I was wondering if there was a way to redirect example.com to a specific directory if the url format is matched with this:

www.example.com/YYYY/MM/
www.example.com/2019/01/article-title

will bring me to the foo directory while any other format brings me to the bar directory.

I have the current code to redirect from public_html to another folder but no idea on how to make a if statement

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteCond %{REQUEST_URI} !^/wp/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /wp/$1
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteRule ^(/)?$ wp/index.php [L] 
</IfModule>

Thank you in advance.

Upvotes: 0

Views: 96

Answers (1)

arkascha
arkascha

Reputation: 42885

The specific rule to forward requests to such pattern looks pretty straight forward:

RewriteEngine on
RewriteRule ^/?(\d+/\d+/.*)$ /foo/$1 [END]
RewriteRule ^/?(.*)$ /bar/$1 [END]

If you want to be really specific about the exact format then this might be closer:

RewriteEngine on
RewriteRule ^/?(\d\d\d\d/\d\d/.*)$ /foo/$1 [END]
RewriteRule ^/?(.*)$ /bar/$1 [END]

Both variants will internally rewrite /2019/01/article-title to /foo/2019/01/article-title. I assume that is what you actually want to achieve. All requests to URLs not matching that initial rule will get rewritten to /bar/....

Upvotes: 1

Related Questions