2Fast4YouBR
2Fast4YouBR

Reputation: 1042

.htaccess rewrite URI w/ replace part of it

Today I have

https://www.teste.com/app/assets/folderX/file.pdf

But I need to redirect to:

https://assets.teste.com/folderX/file.pdf

I got close to the solution with:

RewriteCond %{REQUEST_URI} ^/app/assets/ [NC]
RewriteRule ^(.*)$ https://assets.teste.com/$1 [R=301,L,QSA,NC]

But the rewrite ended like:

https://assets.teste.com/app/assets/folderX/file.pdf

On the ReWrite I need to remove the subfolder /app/assets/ from it.... but I have no idea how.


After some tries. could do it using:

RewriteCond %{REQUEST_URI} ^/app/assets/ [NC]
RewriteRule ^/?app/assets/(.*)$ https://assets.teste.com/$1 [R=301,L]

I nice tool to test is https://htaccess.madewithlove.be/

Upvotes: 1

Views: 35

Answers (1)

anubhava
anubhava

Reputation: 785266

You may use this rule instead:

RewriteCond %{HTTP_HOST} ^(?:www\.)?(teste\.com)$ [NC]
RewriteRule ^(app)/assets/(.*)$ https://$1.%1/$2 [R=301,L,NE,NC]

Make sure to use a new browser to test this change or completely clear old browser cache.

Upvotes: 1

Related Questions