WimDiesel
WimDiesel

Reputation: 65

htaccess redirect if parameter exists not working

I'm trying to rewrite any URL if a special parameter exists.

So that this happens:

From: www.example.com/somepage/someother/?entryValue=somevalue

To: www.example.com/somepage/someother/?product=test&special=12345ls&linkSource=website

I tried to following, but it doesnt work as expected: This code adds var/www/* instead of the link www.example.com/var/www/web6/htdocs/example.com/index.php/*

RewriteCond %{QUERY_STRING} (^|&)entryValue=somevalue
RewriteRule ^(.*)$ $1/?product=test&special=12345ls&linkSource=website [L,R]

This code removes the path:

RewriteCond %{QUERY_STRING} (^|&)entryValue=somevalue
RewriteRule ^(.*)$ /?product=test&special=12345ls&linkSource=website [L,R]

How can I make it work?

Upvotes: 1

Views: 697

Answers (1)

MrWhite
MrWhite

Reputation: 45829

RewriteCond %{QUERY_STRING} (^|&)entryValue=somevalue
RewriteRule ^(.*)$ $1/?product=test&special=12345ls&linkSource=website [L,R]

You need to include a slash prefix at the start of the substitution string. Like this:

RewriteRule ^(.*)$ /$1/?product=test&special=12345ls&linkSource=website [L,R]

Without the slash prefix (the URL-path matched by the RewriteRule pattern does not include a slash prefix) it is seen as relative and the directory-prefix (ie. /var/www/...) will be added back and result in the malformed redirect you are seeing.

UPDATE:

but this ends up with "index.php" and the path is lost

You've put the directive in the wrong place and have a conflict. The order of the mod_rewrite directives is important.

Generally, external redirects like this need to go near the top of the .htaccess file, before any internal rewrites (like a front-controller).

Upvotes: 2

Related Questions