I have a query string like
search.php?id=12&keyword=abc&api=gIUTG6898
And I want the URL to be like this:
search/?id=12&keyword=abc&api=gIUTG6898
Now I found a lot of solutions but they are limited to only one variable in the query string. Thanks in advance
It's not that complicated, since you just want to 'transfer' the query string. You can ignore it in the RewriteRule.
RewriteRule ^search/?$ search.php [QSA]
This just rewrites 'search/' to 'search.php'.
Since you are wanting to remove the .php and replace it with a slash, you'd need to reverse Floern's RewriteRule somewhat:
RewriteRule ^/search.php$ /search/ [QSA,L]
Related
Suppose I want to rewrite from
http://example.com/test?data=abcxyz
to
http://example.com/index.php?module=test&data=abcxyz
I tried
RewriteRule ^test?(.*)$ index.php?module=test&$1 [L]
But it doesn't work, the QUERY_STRING becomes module=test&s. (The whole string data=abcxyz become s).
How can I accomplish this task?
Many thanks in advance!
There is a flag to this, the QSA flag :)
RewriteRule ^test?(.*)$ index.php?module=test&$1 [L,QSA]
Explanation from here : Apache doc
When the replacement URI contains a query string, the default behavior
of RewriteRule is to discard the existing query string, and replace it
with the newly generated one. Using the [QSA] flag causes the query
strings to be combin
My aim is this URL:
component/users/?view=registration
To:
registration.html
the .htaccess is in the folder mysite of the website.
i tried this:
RewriteBase /mysite
RewriteRule ^component/users/?view=registration$ registration.html$ [R=301,L]
But i doesnt work...
When i try this:
RewriteRule ^component/users/_view=registration$ registration.html$ [R=301,L]
it works very well.
So how can i fix this problem with the question mark. I already read thats it is not a part of the URL (its appended). I have read that i have to use something like querystring, but i didn't really understand the syntax.
Maybe someone could write the solution of this problem? Would be awesome =)
You need to use %{QUERY_STRING} to capture the query string data:
RewriteCond %{QUERY_STRING} ^view=(.*)$
RewriteRule ^component/users/?$ %1.html? [R=301,L]
The above rule/condition will take the value of the query string view and use it to form your redirect if the path component/users matches.
RewriteRule ^component/users/\?view=registration$ registration.html$ [R=301,L]
You need a \ because ? is part of a regular expression. To Use it as a string u need to escape it with a \
Linktip: http://ole.michelsen.dk/tools/regex.html
I'm trying to come up with some mod_rewrite to translate http://example.com/?7gudznrxdnu into http://example.com/view.php?id=7gudznrxdnu
But any other page will function properly such as http://example.com/contact and so on.
I think this will work:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^[a-z0-9]+$
RewriteRule ^$ view.php?id=%{QUERY_STRING} [L]
If you want the rewrite to be shown in the browser's address field, you'll have to replace [L] with [L,R=301].
Explanation: The query-string (what's following the question mark) is not part of the URL that RewriteRule sees in its matching-pattern, therefore you can't check for question mark there. In my solution, I run the rule if and only if (RewriteCond) the query string consists solely of a-z and/or 0-9, and my rule only rewrites URLs ending with a slash (except for the query string). I redirect this to view.php?id=, and then append the query string to that.
Edit: Tested on my Apache-server, and I haven't found any bugs (yet).
You should try (in your .htaccess):
RewriteEngine On
RewriteRule ^\?([^/\.]+)?$ view.php?id=$1 [L]
I am trying to rewrite this url and it just doesn't seem to be working and I am unclear what is going wrong
I want this URL:
www.mysite.com/dvd-123/Studio/studio_name.php?thumbs=yes&order=price
To actually send to:
www.mysite/cat/list_products.php?studio=123&store=dvd&thumbs=yes&order=price
Here is what I have that is sorta working:
RewriteRule ^dvd-([0-9]+)/Studio/(.*)\.php?(.*)$ cat/list_products.php?studio=$1&store=dvd&$3 [L]
The results I am receiving are the same as if i want to:
www.mysite/cat/list_products.php?studio=123&store=dvd
ignoring the:
&thumbs=yes$order=price
Thank you in advance for your help!
From Apache documentation:
The Pattern will not be matched against the query string... To combine a new query string with an old one, use the [QSA] flag.
I haven't tested this, but hopefully it should point you in the right direction.
RewriteRule ^dvd-([0-9]+)/Studio/(.*)\.php$ cat/user_list_products.php?Manname=$1&store=dvd [L,QSA]
You need to escape the slashes.
RewriteRule ^dvd-([0-9]+)\/Studio\/(.*)\.php?(.*)$ cat/user_list_products.php?Manname=$1&store=dvd&supersale=$3 [L]
What is wrong with this rewrite rule?
RewriteRule ^api/(.+)$ api/index.php?url=$1 [L]
I simply want "index.php?url=" to be added after api/ and before the rest of the get parameters.
api/image/upload&arg1=1&text=lorem+ipsum
to
api/index.php?url=image/upload&arg1=1&text=lorem+ipsum
What is wrong with (.+) to get everything after api/?
The regex on the RewriteRule is only run against the path part of the URL, not the query parameters. Fortunately there is the [QSA] flag to preserve existing query parameters.
Are you doing something to stop infinite recursion?
RewriteRule ^api/(.+)$ api/index.php?url=$1 [R=301,L]
or some equivalent
I think you must write your domain name before the regex stuff. Like this:
RewriteRule ^(.+).com/api/(.*)$ "$1.com/api/index.php?url=$2" [R=301,L]