Silver Ringvee
Silver Ringvee

Reputation: 5535

Conditional URL rewrite

I need the URL example.com/app to show the content from example.com/app/dist

The following code does exactly this.

RewriteEngine On
RewriteRule ^$ /app/dist/ [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/dist/
RewriteRule ^(.*)$ /app/dist/$1

BUT I also need the URL example.com/staging to show the content from example.com/staging/dist

With the current setup, the url example.com/staging is showing the content from example.com/app/dist

The .htaccess while is located in the /app and /staging folder (but it has to be the same file)

Upvotes: 0

Views: 45

Answers (1)

Jon Lin
Jon Lin

Reputation: 143886

In the htaccess file in your staging folder, change all instances of app to staging

Edit: You can try using relative paths instead of hardcoding the app or staging, but Ive had issues in the past, especially if there are other rules in play.

RewriteEngine On 

RewriteRule ^dist -  [NC,L] 

RewriteRule ^$ dist/ [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ dist/$1 [L]

The idea is to remove the leading / from rewrites so a relative path is used. So technically it wont matter whatever folder the htaccess file is in

Upvotes: 2

Related Questions