Variables disappear on url rewrite with .htaccess - .htaccess

I'm completely stuck with .htaccess trying to rewrite requests so :
website.com/SOMEPAGE
website.com/SOMEPAGE/?arg1=a&arg2=b&arg3
...becomes...
website.com/index.php?page=SOMEPAGE
website.com/index.php?page=SOMEPAGE&arg1=a&arg2=b&arg3
My .htaccess looks like this :
RewriteEngine On
RewriteRule ^([a-zA-Z_-]+)\/?(\?(.+))?$ index.php?page=$1&$3 [NC,L]
And then in index.php :
<script>
var vars = '<?php print(json_encode($_GET)); ?>';
console.log(vars);
</script>
The console only ever logs :
{"page":"SOMEPAGE"}
arg1, arg2 etc. have disappeared.
What am I doing wrong ?

You never receive the query string in your RewriteRule directive. To forward the previous query params after a rewrite, you use the QSA flag:
RewriteEngine On
RewriteRule ^([a-zA-Z_-]+)/?$ index.php?page=$1 [NC,L,QSA]
and that will take care of forwarding your request parameters.

Related

how to Rewrite Single Page url in .htaccess

I need to rewrite only 1 specific URL
from
http://www.domainname.com/index.php?route=payment/axis/callback
to
http://www.domainname.com/payment/axis/callback
I tried these two from stack overflow, I don't know why its not working
1st one :
RewriteRule ^([A-Za-z0-9-]+)/?$ index.php?route=payment/axis/callback [NC,L]
2nd one :
RewriteRule ^index.php?route=payment/axis/callback payment/axis/callback [L]
Try this:
4) Rewriting yoursite.com/user.php?username=xyz to yoursite.com/xyz
Have you checked zorpia.com.If you type http://zorpia.com/roshanbh233 in browser you can see my profile over there. If you want to do the same kind of redirection i.e http://yoursite.com/xyz to http://yoursite.com/user.php?username=xyz then you can add the following code to the .htaccess file.
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ user.php?username=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ user.php?username=$1
See the full page here.
Hope it helps!
I wouldn't use .htaccess with RewriteRule, since there are often problems with it. A simple workaround (with PHP redirect):
<?php
if($_GET['route'] == 'payment/axis/callback') {
header("Location: http://www.domainname.com/payment/axis/callback");
}
?>
You can either use h0ch5tr4355's workaround or you can try this:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^route=payment/axis/callback$
RewriteCond %{SCRIPT_FILENAME} index.php
RewriteRule (.*) /payment/axis/callback [NC,QSD]
If you instead of a rewrite would like it to redirect to the new url you can add R=301 to the flags of the RewriteRule.

Rewrite .htaccess without query string

My URL Syntax Now is Like
http://20percents.com/au/restaurant/?shop=Fantasia%20MacArthur%20Central
And, how can I rewrite it to like
http://20percents.com/au/Fantasia-MacArthur-Central
Remove restaurant from url, remove ?shop from url, and change the %20 to -
I tried like
RewriteEngine on
RewriteRule ^restaurant/([^/\.]+)/?$ restaurant?shop=$1 [L]
This didn't work
I also tried like, and this too didn't work.
RewriteCond %{QUERY_STRING} val
RewriteRule ^/restaurant/?shop
Thanks

Forwarding query string with rewrite rule

I'm trying to write a rewrite rule but I've some serious problems with forwarding query string.
I also tried [QSA] flag but it didn't work.
Following the correct request:
/localhost/registry/registry.php?id=something&password=somethingelse&uri=whatever
And I would change into something like this:
/localhost/registry/register
with the same post query (id=something&password=somethingelse&uri=whatever).
At this point, my .htaccess would modify the above request in the following one:
/localhost/registry/registry.php?request=register&id=something&password=somethingelse&uri=whatever
Then, I tried the following rewrite rule in my .htaccess:
RewriteRule ^register$ http://localhost/registry/registry.php?request=$0&$1 [QSA]
But, after the last '&', nothing is showed and the $1 variable is empty.
What kind of rewrite rule I have to write?
Thanks in advance.
!!! SOLUTION !!!
RewriteRule ^register$ /registry/registry.php?request=$0 [QSA]
RewriteRule ^register$ /registry/registry.php?request=$0 [QSA]
if you go to http://localhost/registry/register?id=1, then query string would be(from php) :
array(2) {
["request"]=>
string(8) "register"
["id"]=>
string(1) "1"
}

Rewrite url with query string in htaccess

Today I try to rewrite some ugly url in order to be cache by browser! But the problem is that I have multiparameters inside the url. An example is better than a long text :
the actual url with the ? and the & :
http://images.mydomain.com/lib/simple-thumb.php?src=http://google.com&l=180&h=135&zc=1
And I want to use this instead :
http://images.mydomain.com/lib/http://google.com/180/135/1
Should I use the rule below in my .htaccess?
Options +FollowSymLinks
RewriteEngine On
rewritecond %{query_string} ^(.*)$
rewriterule simple-thumb\.php /lib/%1/? [R=301,L]
But not seams to be work...
Thanks for your kind help
Try
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^/lib/http\:\/\/google.com/([0-9]+)/([0-9]+)/([0-9]+)$ /lib/simple-thumb.php?src=http://google.com&l=$1&h=$2&zc=$3

Problems with url rewrite of query string

I'm trying to rewrite urls like http://www.url.com/blog/?p=123 to http://www.url.com/#blog/123. I've read up on things and found that you can only parse the query string in the RewriteCond so I tried something like:
RewriteCond %{QUERY_STRING} ^p=([0-9]*)$
RewriteRule ^.*$ /#blog/%0 [NE,R]
When I try this the urls end up being rewritten to:
http://www.url.com/#blog/p=213?p=213
Any ideas how to do this properly? Also, is there a way to add an additional RewriteCond that checks that the REQUEST_URI contains blog?
You can do this by removing the query string entirely:
RewriteCond %{QUERY_STRING} ^p=([0-9]*)$
RewriteRule ^.*$ /#blog/%1? [NE,R]
This should give you:
http://www.url.com/#blog/213
If you want to check if the URL contains the term "blog" then simply check:
RewriteCond %{REQUEST_URI} .*/blog/.*
It is important to note that you will not be able to check for "blog" in links like http://www.url.com/#blog because, as noted by Patrick, everything after the # is not sent to the server.
See the Apache wiki on mod_rewrite for more information.
That may not work because browsers do not send the fragment of the url after the hash (#) with the request, so any request for http://www.url.com/#blog/p=213?p=213 will be a request for http://www.url.com/
The hash fragment is supposed to be used for anchor tags on pages, and is never sent to the server.
Try this rule in your .htaccess file:
Options +FollowSymlinks -MultiViews
RewriteEngine on
RewriteCond %{QUERY_STRING} ^p=(.*)$ [NC]
RewriteRule ^blog/?$ /#blog/%1? [NC,NE,L,R]
With above a URL of http://localhost/blog/?p=1234 will become http://localhost/#blog/1234

Resources