Jared Eitnier
Jared Eitnier

Reputation: 7152

htaccess redirect dynamic url with no query string to query string version

I need to redirect this:

http://domain.com/anotherdomain.com

to

http://domain.com/subfolder/?website=anotherdomain.com

The URL anotherdomain.com will be dynamic. It should only detect domains entered after the root domain and not other subfolders. Ex:

SHOULD REDIRECT

domain.com/anotherdomain.com

SHOULDN'T REDIRECT

domain.com/internet-marketing-services/search-engine-optimization-services/

What I've tried:

<ifModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^(/|index.php)?$ /link-building-services/?website=%1 [R=301,L]
</IfModule>

Upvotes: 1

Views: 123

Answers (1)

anubhava
anubhava

Reputation: 785781

Reason why it is not working is because %1 is the matching group captured in RewriteCond and you aren't capturing anything in your RewriteCond. Variables captured in RewriteRule are denoted by $1, $2, $3 etc.

Replace your code with this:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^([^.]+\..+)$ /link-building-services/?website=$1 [R=301,L]

Upvotes: 1

Related Questions