Anthon95
Anthon95

Reputation: 27

mod_rewrite: Exact URL not matching but matches non exact

I am having issues while using mod_rewrite in an .htaccess file to redirect to another domain.

Here's what I have:

RewriteCond %{HTTP_HOST} ^www.somedomain.com/events_detail/an-event [NC]
RewriteRule ^(.*)$ http://otherdomain.com/directory/8559 [L,R=301,NC]

RewriteCond %{HTTP_HOST} ^www.somedomain.com [NC]
RewriteRule ^(.*)$ http://otherdomain.com/directory [L,R=301,NC]

For some reason www.somedomain.com/events_detail/an-event I get redirected to http://otherdomain.com/directory instead of http://otherdomain.com/directory/8559

When testing out my .htaccess on a tester, it indicates that it does not match the first condition but matches the second. I have tried removing the L option, removing the forward ^ without success.

Thank you

Upvotes: 1

Views: 79

Answers (2)

Amit Verma
Amit Verma

Reputation: 41219

RewriteCond %{HTTP_HOST} ^www.somedomain.com$ [NC]
RewriteCond %{THE_REQUEST} /an-Event [NC]
RewriteRule ^(.*)$ http://otherdomain.com/directory/8559 [L,R=301,NC]

RewriteCond %{HTTP_HOST} ^www.somedomain.com$ [NC]
RewriteRule ^(.*)$ http://otherdomain.com/directory [L,R=301,NC]

Upvotes: 1

Eric J. Francois
Eric J. Francois

Reputation: 682

As far as I can see, HTTP_HOST contains only the host name (e.g. www.somedomain.com), and not the complete url. Have you tried using REQUEST_URI or SCRIPT_FILENAME instead?

Upvotes: 0

Related Questions