Vincent Cueto
Vincent Cueto

Reputation: 225

My htaccess redirect don't work when i specify a word to match

I am locked since hours on a rewrite rule i spend a lot of time looking old questions in stackoverflow and other blogs but i still don't understand ...

I just want to redirect users entering a specific URL containing word investment

So i did this for example :

RewriteRule ^(.*)investment(.*)$ https://example.com/$1 [L,R=301]

And then nothing happens but when i try this :

RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]

The rewrite rule work ... But that is not what i want ^^

my original htaccess is :

# BEGIN WordPress
# Les directives (lignes) entre 'BEGIN WordPress' et 'END WordPress' sont
# généré dynamiquement, et ne doivent uniquement être modifiées via les filtres WordPress.
# Toute modification des directives entre ces marqueurs sera outrepassée.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

I am really lost on how does this rewrite works, if someone know why my redirect is not working, it would be cool ^^

Thanks in advance all !

Upvotes: 1

Views: 200

Answers (1)

anubhava
anubhava

Reputation: 785276

Probably you have some other rule(s) modifying REQUEST_URI. You can try this rule with THE_REQUEST variable

RewriteCond %{THE_REQUEST} \s/+(.*?)investment [NC]
RewriteRule ^ https://example.com/%1 [L,R=301]

THE_REQUEST variable represents original request received by Apache from your browser and it doesn't get overwritten after execution of other rewrite directives. Example value of this variable is GET /index.php?id=123 HTTP/1.1


After latest edit of question:

You can insert this rule:

RewriteRule ^(.*)investment https://example.com/$1 [L,R=301,NC]

just below RewriteBase line and retest.

Upvotes: 1

Related Questions