H. Ferrence
H. Ferrence

Reputation: 8116

Need mod_rewrite condition and rule to replace ".com" with ".org" on 1 virtual host

I have a series of mod_rewrite directives (shown below as Code 1) that run every server request through a custom PHP app.

Code 1

Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /app/core.php [L,NC,QSA]

Before I get to the last step in the mod_rewrites, I need to change any request to mydomain.com to mydomain.org. Code 2 below shows what I am thinking but it does not work. The request gives me 500 Internal Server error.

Code 2

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^mydomain\.com$
RewriteRule ^ http://mydomain.org%{REQUEST_URI}
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /app/core.php [L,NC,QSA]

Can someone offer a suggestion? Thanks

Upvotes: 0

Views: 343

Answers (2)

Chronial
Chronial

Reputation: 70763

I asume your site is reachable by two domains, but you want all request to your site redirected to one of them. Then the correct code is:

RewriteCond %{HTTP_HOST} !^mydomain\.com$ [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^(.*)$ http://mydomain.org/$1 [R=permanent,L]

You should place that code directly after the redirect line.

The second line makes sure that there is no redirect if no Host is given (to prevent a redirect loop in that case).

The [R=permanent,L] in the third line makes it a permanent redirect and prevents any further rule processing. The other rules will be processed once the redirect has taken place.


The full file would then be:

Options +FollowSymLinks
RewriteEngine on
RewriteBase /

RewriteCond %{HTTP_HOST} !^mydomain\.com$ [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^(.*)$ http://mydomain.org/$1 [R=permanent,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /app/core.php [L,NC,QSA]

Upvotes: 1

H. Ferrence
H. Ferrence

Reputation: 8116

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} !^mydomain\.com$ [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^(.*)$ http://mydomain.org/$1 [R=permanent,L]
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /app/core.php [L,NC,QSA]

Upvotes: 0

Related Questions