Expand my Community achievements bar.

SOLVED

Multiple Rewrite rules for a group of RewriteCond

Avatar

Level 3

Hi,

 

I'm working on a site that has both EN and ES versions of pages. The rewrite rules are configured at the dispatcher and I need 2 rewrite rules to be executed for a group of RewriteConds.

 

Below is a sample htaccess file which works fine

 

RewriteCond %{REQUEST_URI} !^/cond1
RewriteCond %{REQUEST_URI} !^/cond2

RewriteCond %{REQUEST_URI} !^/cond3
RewriteRule ^/(.*)$ /content/appName/us/en/home/$1.html [PT,L]

 

I would like to have the ES version of the rule added to this same condition. 

 

RewriteCond %{REQUEST_URI} !^/cond1
RewriteCond %{REQUEST_URI} !^/cond2

RewriteCond %{REQUEST_URI} !^/cond3

RewriteRule ^/es/(.*)$ /content/appName/us/es/home/$1.html [PT,L]
RewriteRule ^/(.*)$ /content/appName/us/en/home/$1.html [PT,L]

 

Looks like we cannot have RewriteCond have multiple rules unless we want to negate and use Skip Rules as below but this doesn't work for me

 

RewriteCond %{REQUEST_URI} !^/cond1
RewriteCond %{REQUEST_URI} !^/cond2

RewriteCond %{REQUEST_URI} !^/cond3

RewriteRule .? - [S=2]

RewriteRule ^/es/(.*)$ /content/appName/us/es/home/$1.html [PT,L]
RewriteRule ^/(.*)$ /content/appName/us/en/home/$1.html [PT,L]

 

Repeating the condition twice as below seems to work fine but I'm looking for the best solution without having to repeat the same thing.

RewriteCond %{REQUEST_URI} !^/cond1
RewriteCond %{REQUEST_URI} !^/cond2

RewriteCond %{REQUEST_URI} !^/cond3

RewriteRule ^/(.*)$ /content/appName/us/en/home/$1.html [PT,L]

 

RewriteCond %{REQUEST_URI} !^/cond1
RewriteCond %{REQUEST_URI} !^/cond2

RewriteCond %{REQUEST_URI} !^/cond3

RewriteRule ^/es/(.*)$ /content/appName/us/es/home/$1.html [PT,L]

 

Can somebody assist on what would be the best way to handle this? Thanks in advance.

 

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi @manasabojja7 ,

If you have two language sites and you want to go ahead with your approach then I'd suggest you write rewrite rule first for root path then you can write for other.

Below is just a sample rule, please tweak it based on your project

# handle root path.
RewriteRule ^/$ /content/ab/en.html [PT,L]
RewriteRule ^/en.html$ / [R=302,L,NC]
RewriteRule ^/es.html$ / [R=302,L,NC]
RewriteRule ^/content/ab/es.html$ /content/ab/es.html [R=302,L,NC]

 

Then in subsequent rule set, lets say for image:
For es:-

RewriteCond %{REQUEST_URI} !^/content/dam
RewriteCond %{REQUEST_URI} !^/etc
RewriteCond %{REQUEST_URI} \.(jpg|jpeg|png|gif|svg)$ [NC]
RewriteCond %{REQUEST_URI} ^/es
RewriteRule ^/(.*)$ /content/ab/$1 [PT,L]

 For content path

 

# handle valid content paths for ES
RewriteCond %{REQUEST_URI} ^/es
RewriteCond %{REQUEST_URI} !^/apps
RewriteCond %{REQUEST_URI} !^/bin
RewriteCond %{REQUEST_URI} !^/content
RewriteCond %{REQUEST_URI} !^/etc
RewriteCond %{REQUEST_URI} !^/home
RewriteCond %{REQUEST_URI} !^/libs
RewriteCond %{REQUEST_URI} !^/etc
RewriteCond %{REQUEST_URI} !^/saml_login
RewriteCond %{REQUEST_URI} !^/system
RewriteCond %{REQUEST_URI} !^/tmp
RewriteCond %{REQUEST_URI} !^/var
RewriteCond %{REQUEST_URI} .html
RewriteRule ^/(.*)$ /content/ab/$1 [PT,L]

 

Thanks
Tarun

View solution in original post

4 Replies

Avatar

Correct answer by
Community Advisor

Hi @manasabojja7 ,

If you have two language sites and you want to go ahead with your approach then I'd suggest you write rewrite rule first for root path then you can write for other.

Below is just a sample rule, please tweak it based on your project

# handle root path.
RewriteRule ^/$ /content/ab/en.html [PT,L]
RewriteRule ^/en.html$ / [R=302,L,NC]
RewriteRule ^/es.html$ / [R=302,L,NC]
RewriteRule ^/content/ab/es.html$ /content/ab/es.html [R=302,L,NC]

 

Then in subsequent rule set, lets say for image:
For es:-

RewriteCond %{REQUEST_URI} !^/content/dam
RewriteCond %{REQUEST_URI} !^/etc
RewriteCond %{REQUEST_URI} \.(jpg|jpeg|png|gif|svg)$ [NC]
RewriteCond %{REQUEST_URI} ^/es
RewriteRule ^/(.*)$ /content/ab/$1 [PT,L]

 For content path

 

# handle valid content paths for ES
RewriteCond %{REQUEST_URI} ^/es
RewriteCond %{REQUEST_URI} !^/apps
RewriteCond %{REQUEST_URI} !^/bin
RewriteCond %{REQUEST_URI} !^/content
RewriteCond %{REQUEST_URI} !^/etc
RewriteCond %{REQUEST_URI} !^/home
RewriteCond %{REQUEST_URI} !^/libs
RewriteCond %{REQUEST_URI} !^/etc
RewriteCond %{REQUEST_URI} !^/saml_login
RewriteCond %{REQUEST_URI} !^/system
RewriteCond %{REQUEST_URI} !^/tmp
RewriteCond %{REQUEST_URI} !^/var
RewriteCond %{REQUEST_URI} .html
RewriteRule ^/(.*)$ /content/ab/$1 [PT,L]

 

Thanks
Tarun

Avatar

Administrator

@manasabojja7 Did you find the suggestions from users helpful? Please let us know if more information is required. Otherwise, please mark the answer as correct for posterity. If you have found out solution yourself, please share it with the community.



Kautuk Sahni