Reputation: 71
I want to redirect using mod_rewrite all URI's ending with a known name, but the subfolders are not known. Something like this:
From:
example.com/random1/random2/my-text
To:
example.com/my-text/
random1
and random2
are not known. I tried .*/.*/^my-text$
and many variants but I can't get it to work.
Any ideas?
UPDATE: Existing .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Upvotes: 2
Views: 28
Reputation: 45829
To redirect all URLs that end with my-text
using mod_rewrite in .htaccess, try the following:
RewriteEngine On
RewriteRule .+/(my-text)$ /$1/ [R=302,L]
Note that the source URL does not end with a slash, but the destination does (as per your example).
Change the 302
(temporary) redirect to a 301
(permanent) when you are sure it's working OK - assuming this should be a permanent redirect?
Note that external redirects should generally go before any existing directives in your .htaccess file that rewrites the request. (So, the above should go immediately after the RewriteBase
directive in your existing .htaccess file. RewriteEngine
only needs to appear once.)
Upvotes: 1