First of all, I have a link that takes 2 parameters;
http://localhost/project/home.php?SK=2&referance=1
?SK=2 is my first parameter and &referance=1 is my second parameter. The SK=2 parameter draws my files in the php file, and the referance=1 parameter prints the value from the get method to my registration page. What I want is to put this link in below format with htaccess
http://localhost/project/register?referance=1
The link I made before brings my register page, but now I want it to bring the reference code, but unfortunately it doesn't.
Htaccess code I used before;
RewriteRule ^register$ home.php?SK=2 [NC,L]
RewriteRule ^register$ home.php?SK=2 [NC,L]
You just need to add the QSA (Query String Append) flag to your existing rule.
For example:
RewriteRule ^register$ home.php?SK=2 [QSA,NC,L]
This will now rewrite /register to /home.php?SK=2 and /register?referance=1 to /home.php?SH=2&referance=1.
Reference:
http://httpd.apache.org/docs/2.4/rewrite/flags.html#flag_qsa
Related
On the home page of my website, I have a guide that includes some links, and each redirects to a family guide page, all with the redirection by identification of each element of the guide, remaining as the link
https://example.com/family-guide/?id=4
Where '4' is the ID of the element.
I would like to leave like this:
https://example.com/family-guide/4
I tried to use mod_rewrite in the .htaccess file:
RewriteRule ^family-guide/([0-9] +)/([0-9a-zA-Z_-]+) family-guide/?Id=$1name=$2 [NC, L]
but nothing happens
Your rule has some spaces. Be careful because if there is a space between the flags [NC, L] the rule is not valid.
Another thing is that you want the url https://example.com/family-guide/4 to match a rule and your rule expects two parameters (id and name) while in this url there is only one.
I would use a couple of rules instead:
RewriteRule family-guide/([0-9]+)/([0-9a-zA-Z_-]+) family-guide/?id=$1&name=$2 [NC,L]
RewriteRule family-guide/([0-9]+) family-guide/?id=$1 [NC,L]
With this rules if you go to https://example.com/family-guide/4 you should be shown the content of https://example.com/family-guide/?id=4. And if you go to https://example.com/family-guide/4/whatever it will show you the content of https://example.com/family-guide/?id=4&name=whatever instead, which is what I understood you need.
Also, the rule order is important as if they were in the opposite order https://example.com/family-guide/4/whatever would match the other rule and show the content of https://example.com/family-guide/?id=4/whatever
I've a file sharing script, When I upload a file, the download link of the file is showing as this :
http://www.example.net/file?id=fileID
Noting that the ( file?id=fileID ) is given by a functions php file .. while the original shape of links : download.php?id=fileID.
Now when I browse the link http://www.example.net/file?id=fileID ... I get Not found .. and the htaccess has the following :
RewriteRule ^file([0-9]*)?id=$ download.php?id=$1
and it still not found..
So I want to Rewrite the original links to work when I browse the link:
http://www.example.net/file?id=fileID
This should work
RewriteRule ^file/? download.php [QSA]
I used the QSA flag because it retains the query string and passes it on to the new URL. By default, RewriteRule will discard the query string.
Edit:
Based on your comment, use this rule to achieve what you want.
RewriteCond %{QUERY_STRING} ^(.*)img(.*)$
RewriteRule ^download.php/? img.php?%1id%2 [QSA]
I've following 2 rules in my .htaccess file -
1. RewriteRule ^myscript/([A-Za-z0-9]{0,1})\?page=([0-9]?)$ /myscript.php?action=check&var=$1&page=$2 [L]
2. RewriteRule ^myscript/([A-Za-z0-9]{0,1})$ /myscript.php?action=check&var=$1 [L]
so that visitng /myscript/d sends a request as /myscript.php?action=check&var=d
I am trying to add an option page parameter so that visiting /myscript/d?page=5 sends the requests as /myscript.php?action=check&var=d&page=5
to achieve this I tried this
RewriteRule ^myscript/([A-Za-z0-9]{0,1})\?page=([0-9]?)$ /myscript.php?action=check&var=$1&page=$2 [L]
But this rules is being ignored and the request is sent as /myscript.php?action=check&var=d (i.e. 2nd rule from the above is being applied). What am I doing wrong here? What changes do I need to make it get it working?
Thanks for your help.
Use 2nd rule only and add QSA flag:
RewriteRule ^myscript/([A-Za-z0-9]{0,1})$ /myscript.php?action=check&var=$1 [QSA,L]
This will pass existing query parameter to the new URL:
/myscript/d?page=5&say=hello
=>
/myscript.php?action=check&var=d&page=5&say=hello
Apache documentation: http://httpd.apache.org/docs/current/rewrite/flags.html#flag_qsa
currently I m using following code in my site in .htaccess file :
RewriteRule ^([^/\.]+).php?$ comman.php?cat=$1 [L]
This redirects user to comman.php page say, user requests
http://www.mysite.com/myfolder/page1.php
will redirects to
http://www.mysite.com/myfolder/comman.php?cat=page1
This works fine. My question is how can I achieve following
http://www.mysite.com/myfolder/page1.php?var1=123
to redirect
http://www.mysite.com/myfolder/comman.php?cat=page1¶m=123
i.e. whatever value passed to url using get method to add in my new url.
Thanks in Advance.....
You should add the QSA flag:
RewriteRule ^([^/\.]+).php?$ comman.php?cat=$1 [L,QSA]
QSA stands for Query String Append. Anything after the ? in the original URL will be appended to the rewritten URL.
You need to ass QSA to your rule, i.e.
RewriteRule ^(.*)$ /index.php?pageid=$1 [QSA,L]
You can use the QSA flag in your RewriteRule for that.
See the docs.
I have successfully setup htaccess to do this:
domain.com/ad.php?ad_id=bmw_m3_2498224
INTO:
domain.com/ads/bmw_m3_2498224
However, I have a link on the page which makes the page submit to itself...
The link saves the ad inside a cookie:
domain.com/ad.php?ad_id=bmw_m3_2498224&save=1 // Note the 'save' variable
I need to make this work on the rewritten rule also, so this link:
domain.com/ads/bmw_m3_2498224/save
will save the cookie...
I have this so far which DOES NOT work for the save part:
RewriteRule ^annons/([a-zA-Z0-9_]+)$ ad.php?ad_id=$1 [NC,L]
How can I include another rule to accomplish what I want?
Thanks
A simple way would be to add this line above what you currently have:
RewriteRule ^annons/([a-zA-Z0-9_]+)/save$ ad.php?ad_id=$1&save=1 [NC,L]
RewriteRule ^annons/([a-zA-Z0-9_]+)$ ad.php?ad_id=$1 [NC,L]
This should get triggered before the general rule is and work just the same.