Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rewrite base URL and add a specific word to it using mod_rewrite

below is my requirement : Supposing i have a URL coming from browser

http://test.aroa.com/mance

I need to add /xyz/abc/xy/ before perfomance and .html after it i.e. using re-write rules of mod_rewrite to change it to below URL once it hits the dispatcher in AEM(or apache web-server)

http://test.aroa.com/xyz/abc/xy/mance.html

For this i wrote the below re-write rule along with some rewrite conditions(rewrite conditions not here)

RewriteRule ^(/.*)$ /xyz/abc/xy$1.html [P,L]

It works for the this site but messes up some other functionalities by adding /xyz/abc/xy to other URLs as well

Can you suggest me some way using which i can restrict the URL rewriting only for the //test.aroa.com/ URL and not affect any other URL

I tried putting the rule inside directory tag of with the doc-root name inside it. but it fails to get applied in that case..

Can anyone suggest something that can help

like image 278
user1643087 Avatar asked Oct 30 '22 08:10

user1643087


1 Answers

If you want to apply your rule on a specific domain, you can add this condition

RewriteCond %{HTTP_HOST} ^test\.aroa\.com$ [NC]

Also, a small semantic detail: you don't need L flag with P flag (it's redundant, since P includes L)

Finally, your code should look like this

RewriteCond %{HTTP_HOST} ^test\.aroa\.com$ [NC]
# your other conditions
RewriteRule ^/?(.+)$ /xyz/abc/xy/$1.html [P]

Remark: i think you don't need to use mod_proxy (P flag) especially because it's slower than a simple internal rewrite. If your (sub-)domains share the same document root then you can avoid using it by replacing P flag with L flag

like image 132
Justin Iurman Avatar answered Nov 09 '22 16:11

Justin Iurman