Alan Shortis
Alan Shortis

Reputation: 1109

Conditional redirect in .htaccess using RewriteCond

I am building a directory of my company's projects that will live both locally (using MAMP for the time being) and on our staging server.

What I am trying to do is have a single .htaccess that will conditionally redirect depending on where it's being requested.

I have the first part working fine - from the root of the staging server to our home page.

RewriteEngine On

RewriteCond %{HTTP_HOST} staging.com
RewriteRule (.*) http://www.homepage.com [R=301,L]

This is the part I cannot fathom. If viewing our local server at the root (e.g. http://102.168.0.27:8888) I would like to redirect to a folder on the same domain (e.g. http://102.168.0.27:8888/directory). Viewing any folder (http://102.168.0.27:8888/*) should not redirect, only when no folder is specified.

Too add to the complexity, that IP address could change and I don't want to have to update it. I also don't want to have to specify folders on that domain that shouldn't redirect; it should be ANY folder.

A different redirect locally is easy enough, I just cannot get it to meet my requirements for folders:

RewriteCond %{HTTP_HOST} !staging.com
RewriteRule (.*) /directory [R=301,L]

To summarise:

Upvotes: 0

Views: 556

Answers (1)

yoavmatchulsky
yoavmatchulsky

Reputation: 2960

Using the 8888 port condition for local environment, try this:

RewriteCond %{SERVER_PORT} 8888 RewriteCond %{REQUEST_URI} ^/$ RewriteRule .* /directory [R=301,L]

Other files/directories on the local server shouldn't be affected by this rule.

Upvotes: 1

Related Questions