Rewrite the base URL and add a specific word to it using mod_rewrite

below is my requirement: Suppose I have a URL coming from a browser

http://test.aroa.com/mance 

I need to add / xyz / abc / xy / before perfomance and .html after it, i.e. use mod_rewrite rewrite rules to change it below url as soon as it gets to dispatcher in AEM (or apache web server)

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

To do this, I wrote the following rewrite rule along with some rewrite conditions (rewrite conditions not here)

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

It works for this site, but confuses some other functions by adding / xyz / abc / xy to other URLs as well

Can you suggest me some way by which I can restrict the rewriting of URLs only to the URL // test.aroa.com / and not affect any other URL

I tried to put a rule inside a directory tag named doc-root inside it. but in this case it cannot be applied.

Can someone suggest something that might help

+5
source share
1 answer

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

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

In addition, a small semantic detail: you do not need the L flag with the P flag (it is redundant because 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] 

Note: I think you do not need to use the mod_proxy ( P ) flag, especially because it is slower than a simple internal rewrite. If your (sub) domains have the same document root, you can avoid using it by replacing the P flag with the L flag

+2
source

Source: https://habr.com/ru/post/1243895/


All Articles