0
votes

I want to use mod rewrite to redirect the following dynamic URL with h query string:

https://eu1.domain.com/~username/test.php?h=String_112016

To the following URL without the test.php part:

https://eu1.domain.com/~username/String_112016

I tried the following in my htaccess file but it's getting 404:

RewriteEngine On

RewriteRule ^\~username/([^/]*)$ /~username/test.php?h=$1 [L]

I don't have any other rule in the htaccess file.

2
From the code you wrote, it looks what you want is not actually a redirect. When a user requests eu1.domain.com/~username/String_112016, you want the test.php page to be rendered with the "String_112016" sent as paramater h... is that correct? - VKK

2 Answers

0
votes

From the code you wrote, I'm assuming what you want is not actually a redirect. Correct me if I'm wrong, but what you want is that when a user requests /~username/String_112016, you want the test.php page to be rendered and passed "String_112016" as a parameter labeled h.

You're really really close... Try this:

RewriteEngine On

RewriteRule ^/?~username/([^/]*)$ /~username/test.php?h=$1 [L]

The first matched character in the Rewrite Rule should be a forward slash, not a backslash. This forward slash is practically always present in requests, but I think it's best practice to treat it as optional, because technically you could get a (malformed) request that does not have one. The question mark after the /, makes the / optional.

0
votes
RewriteEngine On
RewriteBase /
RewriteRule ^~username/test.php\?h=(.*)$ /~username/$1 [L]