adding a question mark to the url in url rewrite in .htaccess - php

Hi everybody i am trying to append a question mark at the end of my file name in url rewrite, after which will be my url parameters, like example the expected url is like:
http://localhost/project/pass/?something#gmail.com-3861
and the rule that i am using is below:
RewriteRule ^pass/?(\w.+)-(\w.+)$ view/pass.php?useremail=$1&actcode=$2 [L]
but the ? is not working as i assuming it to be a reserved keyword. If i use some other things like - then they work
Can anyone tell me solution to that so that i can use ? in my url rewrite?

RewriteRule does not accept query. Use RewriteCond
RewriteEngine on
RewriteCond %{QUERY_STRING} (\w.+)-(\w.+)
RewriteRule ^pass/$ view/pass.php?useremail=%1&actcode=%2 [L]

If you want to use reserved characters without theirs function (? stands for "zero times or once"), add backslash \ before them.

Related

Search replace query string with htaccess

I have my site url like this.
https://example.com/page/1/?orderby=low_to_high&s=demo+post&post_type=post&twdg_wsac=1
I want to replace some part of this url using htaccess so the final output should be like this
https://example.com/page/1/?orderby=low_to_high&s=demo+post&post_type=post&custom_posts=1
So I have made changes in the htaccess file like this
RewriteEngine on
RewriteRule ^&twdg_wsac$ &custom_posts
But its not working. Also as you can see in the url there is twdg_wsac=1. So the last "1" is a pagination for post so that would change dynamically as per posts count.
So can someone tell me how to do this?
Any help and suggestions would be really helpful. Thanks
Update
All the parameters in the url are dynamically generated I mean those are the filter parameters. So they cant be same except twdg_wsac
Try with below, we are doing rewrite on query string.
RewriteEngine On
RewriteCond %{QUERY_STRING} ^orderby=(.+)&s=(.+)&post_type=(.+)&twdg_wsac=([\d]+)$
RewriteRule ^ %{REQUEST_URI}?orderby=%1&s=%2&post_type=%3&custom_posts=%4
RewriteEngine On
RewriteCond %{QUERY_STRING} twdg_wsac=([\d]+)$
RewriteRule ^ %{REQUEST_URI}?orderby=low_to_high&s=demo+post&post_type=post&custom_posts=%1 [L]

How to create RewriteRule for space using htaccess

I use htaccess for make mu URL clean. My original ULR is
http://localhost/bookStore/single_book.php?name=This%20is%20book%20no%204
i use php urlencode() function to make this
http://localhost/bookStore/single_book.php?name=This+is+book+no+4
but now i want to make this like:
http://localhost/bookStore/single_book/This+is+book+no+4
using htaccess
RewriteRule ^single_book/([0-9a-zA-Z_-]+) single_book.php?name=$2 [NC,L]
RewriteRule not working
RewriteCond %{QUERY_STRING} ^name=(.+)
RewriteRule ^(bookStore/single_book)(\.php)$ $1/%1?
This matches if name is in the query string and if the name is the only portion of the query string.
I tested on https://htaccess.madewithlove.be/
And for playing with the regex, i used https://regexr.com/
The trailing ? in the RewriteRule removes the query string.

Mod_rewrite with ".html?variable=" not working [duplicate]

This question already has an answer here:
.htaccess RewriteRule to preserve GET URL parameters
(1 answer)
Closed 8 years ago.
I have a website that I successfully use the following url rewrite on:
RewriteRule ^(.*)_r-(.*)/calculator.html$ /calcadd.php?rest_id=$1 [L]
As my website has evolved I've decided it would be helpful to be able to pass some additional parameters to this page. So I tried to update the url to include a variable after the .html. Here's what I tried:
RewriteRule ^(.*)_r-(.*)/calculator.html?ids=(.*)$ /calcadd.php?rest_id=$1&ids=$3 [L]
This doesn't work, I get a page not found error. And just in case it wasn't clear, this is NOT a mod_rewrite isn't enabled or whatever problem. All my other rewrites work just fine, so mod_rewrite is enabled, allow override is ALL, etc.
Thanks!
You just need a QSA flag here to preserve original query while adding new query parameters:
RewriteRule ^(.*)_r-(.*)/calculator\.html$ /calcadd.php?rest_id=$1 [L,NC,QSA]
Also remember that you cannot match query string in RewriteRule.
The RewriteRule only consideres the requested url without the querystring by default. You need a RewriteCond to achieve what you want:
RewriteCond %{QUERY_STRING} (.*)
RewriteRule ^(.*)_r-(.*)/calculator.html$ /calcadd.php?rest_id=$1&%1 [L]
untested.
With %1, %2, etc. you can inject the patterns matched in the correspondent parenthesized patterns in the RewriteCond statement in your RewriteRule.
Just leave it the way it was and use this. QSA flag means Query String Append. It will will automatically add the query sting to the end of your rewrite.
RewriteRule ^(.*)_r-(.*)/calculator.html$ /calcadd.php?rest_id=$1 [QSA,L]

URL rewrite to ignore php $_GET statement

I have a url like:
http://www.site.com/profile.php?id=$id
And I want it to end up like:
http://www.site.com/$id
How could I do this in PHP or in htaccess?
You would need to create a rewrite rule in htaccess such as
RewriteRule ([0-9]+) profile.php?id=$1 [L]
This would rewrite anything with a 0-9 character to profile.php and pass the number in the query string. So you could do
site.com/1
Which would be
site.com/profile.php?id=1
N number of discussion gone already here. Anyway, here it is. :)
Your htacces rewrite rule,
RewriteEngine on
RewriteBase /
RewriteRule ^([0-9]+)$ profile.php?id=$1 [L]
Way to link in your PHP files,
Your URL
Make sure to replace $id with your values.
FYI, http://www.site.com/profile.php?id=$id will also works, avoid using it & use mask URL like i quoted above. :)

How to escape “?” using regex in .htaccess for mod_rewrite

What is wrong in this rule?
RewriteRule ^page\?v=([^/]+)$ page.php?v=$1 [L,NC]
I just want to make the URL looks like that
http://www.domainname.com/page?sk=info
You don't have to include the query parts if they're not changing anyway.
RewriteRule ^page$ page.php [L,NC]
The RewriteRule will not match any part of the query string. page?v=123 will still become page.php?v=123
Also, your RewriteRule uses ?v= while you talk about ?sk=info
Also, you can find additional information about this mod_rewrite case here:
http://wiki.apache.org/httpd/RewriteFlags/QSA
And another SO entry about this issue here.

Categories