Whip
Whip

Reputation: 2224

htaccess - Simple rewrite rule not triggering

I'm trying to match a simple rule to rewrite a url but it's just not matching. I want to redirect https://example.com/web/thanks/ to https://example.com/thanks.php

Here's what I've tried

RewriteEngine On
RewriteBase /
RewriteRule ^thanks/$ https://example.com/thanks.php [R=302,L]

RewriteEngine On
RewriteRule ^thanks/$ https://example.com/thanks.php [R=302,L]

RewriteEngine On
RewriteBase /
RewriteRule ^/(.*)/thanks/$ https://example.com/thanks.php [R=302,L]

RewriteEngine On
RewriteBase /
RewriteRule ^web/thanks/$ https://example.com/thanks.php [R=302,L]

and many more tiny variations but none of them are triggering. I tried using this online tool and it returns "This rule was not met". What am I doing wrong?

Upvotes: 1

Views: 808

Answers (1)

Olaf Dietsche
Olaf Dietsche

Reputation: 74018

To rewrite, just use your last rule

RewriteRule ^web/thanks/?$ /thanks.php [L]

with the following changes

  • no RewriteBase, this is only relevant for some relative URLs

  • optional trailing slash /?, if you want both /web/thanks or /web/thanks/ to work

  • no domain name, because this might trigger a redirect instead of a rewrite, see RewriteRule

    Absolute URL
    If an absolute URL is specified, mod_rewrite checks to see whether the hostname matches the current host. If it does, the scheme and hostname are stripped out and the resulting path is treated as a URL-path. Otherwise, an external redirect is performed for the given URL. To force an external redirect back to the current host, see the [R] flag below.

  • no R|redirect flag, because this triggers a redirect instead of a rewrite


The pattern ^.*thanks/$ or ^(.*)thanks/$ also works, but it matches any URL ending in thanks/, like /hellothanks/, /areyousurethanks/, /some/path/thanks/, ...

Upvotes: 1

Related Questions