I currently have a simple rewrite that redirects
/photos/2
to
/photoviewer.php?photo=2
However I need to be able to allow the user to add a photo to their cart so I was thinking I need to retain the query string so that the following works.
/photos/2?action=purchase
redirects to:
/photoviewer.php?photo=2&action=purcahse
My current htaccess rule is:
RewriteRule ^photos/([a-zA-Z0-9_-]+)$ photoviewer.php?photo=$1 [L]
Append the [QSA] flag (query string append).
RewriteRule ^photos/([a-zA-Z0-9_-]+)$ photoviewer.php?photo=$1 [L,QSA]
In your rule, replace [L] with [L,QSA].
That will then retain the query information.
Hope this helps!
[QSA,L] instead of just [L] should do the trick. Hope this helps!
How about just extending the idea to pass "any" name/value pairs thru the URL to the photoviewer.php script? This is an elegant approach I've used in the past.
RewriteRule ^photos\/([^/\.\-]+)\/([^/\.\-]+)$ /photoviewer\.php\?$1=$2 [L]
RewriteRule ^photos\/([^/\.\-]+)\/([^/\.\-]+)\/([^/\.\-]+)\/([^/\.\-]+)$ /photoviewer\.php\?$1=$2&$3=$4 [L]
RewriteRule ^photos\/([^/\.\-]+)\/([^/\.\-]+)\/([^/\.\-]+)\/([^/\.\-]+)\/([^/\.\-]+)\/([^/\.\-]+)$ /photoviewer\.php\?$1=$2&$3=$4&$5=$6 [L]
So, something like:
photos/photo/2/action/purchase
rewrites to:
photoviewer.php?photo=2&action=purchase
Related
I want to write url-rewrite in php need make such url store/{stre-name}?offer=10 for Example see below
/stores/mango?offer=10
/stores/apple?offer=10
i have write .htacess as
RewriteRule ^stores/(.*)$ offer.php?c=$1&offer=$2 [L]
and write code on offer.php as
$c = $_GET['c'];
$offer = $_GET['offer']
i have got value of c here successfully , but i have not got value of offer here . So please help me how can i read the value of offer in offer.php
RewriteRule ^stores/(.*)$ offer.php?c=$1&offer=$2 [L]
If you want to rewrite /store/mango/?offer=10 to /offer.php?c=mango with the old queryString, You need to use QSA flag in your rule that combines new and old queryStrings
RewriteRule ^store/(.+)/?$ /offer.php?c=$1 [QSA,L]
This will work for you.
RewriteRule ^store/([^/]*)/([^/]*)$ /offer.php?c=$1&offer=$2 [L]
You can try also for make SEO friendly URL
RewriteRule ^store/([^/]*)?([^/]*)\.html$ /offer.php?c=$1&offer=$2 [L]
This code is working for me:
RewriteRule ^stores/(.*)?([^/]*)$ offer.php?c=$1&offer=$2 [QSA,L]
For this URL:
/stores/mango?offer=10
I got the difficult when rewrite rules with multiple parameter,to modify a URL to SEO-friendly.
My URL:
http://domain/cat.php?alias=canon&sort=price&page=3
I want to have a rewrite rule so that the following:
http://domain/c/canon?sort=price&page=3
Heres my current rule:
RewriteEngine On
RewriteRule ^c/([a-z,0-9-]+)$ cat.php?alias=$1 [L]
RewriteRule ^c/([a-z,0-9-]+)?sort=([a-z]+)$ cat.php?alias=$1&sort=$2 [QSA]
RewriteRule ^c/([a-z,0-9-]+)?sort=([a-z]+)&page=([0-9]+)$ cat.php?alias=$1&sort=$2&page=$3 [QSA]
I try to get the params but it doesn't work. Anyone have any ideas on which rewrite rules to use?
Thank you!
--hatxi
RewriteRule ^c/([a-z,0-9-]+) cat.php?alias=$1 [L,QSA]
Should be enough. The QSA flag will take care of passing the sort and page parameters.
Your rules don't work because of the [L] flag on the first one, it just discards the rest because it always matches first.
I have a single get data (affid) that i want to retrieve using .htaccess but i don't know how to do it. Can you help me out?
Here is an example of links:
mysite.com/searchmembers?affid=1001
mysite.com/profle/123?affid=1002
mysite.com/videos/567?affid=1003
Another thing that might give a problem is these links already have been rewritten on .htaccess
RewriteRule ^searchmembers? index.php?task=searchMembers
RewriteRule ^profile/(.*)? index.php?task=viewProfile&id=$1
RewriteRule ^videos? index.php?task=videos&id=$1
i just want to retrieve the affid and add it to the links like this:
RewriteRule ^searchmembers... index.php?task=searchMembers&affid=(data retrieved)
RewriteRule ^profile/(.*)... index.php?task=viewProfile&id=$1&affid=(data retrieved)
RewriteRule ^videos? index.php... task=videos&id=$1&affid=(data retrieved)
i know i could add it on htaccess for each of these links but if there is an easier way to do this then it would be a great help. thank you for any response that i will receive!
Add this to your .htaccess in your web root / directory
RewriteEngine on
RewriteBase /
RewriteRule ^searchmembers$ index.php?task=searchMembers [QSA,NC,L]
RewriteRule ^profile/(.*)$ index.php?task=viewProfile&id=$1 [QSA,NC,L]
RewriteRule ^videos/(.*)$ index.php?task=videos&id=$1 [QSA,NC,L]
If you just need to append a new query parameter (like task here), any existing query parameters can be appended automatically using the [QSA] (Query String Append) flag. I've also corrected the regex used in your RewriteRules. The use of ? is incorrect.
the incoming URL
domain.com/12/3
should be re-written to
domain.com/?w=12&h=3
htaccess
RewriteEngine on
RewriteRule ^([a-z]{2,2})/([a-zA-Z0-9_-]+)$ index.php?w=$1&h=$2 [QSA]
the php
<?php
echo $_GET['h'];
?>
Result
404 page
I've tried using htaccess to change the result of the url and then retrieve the value from the URL, could someone help me out?
Maybe you need to escape - like:
RewriteRule ^([a-z]{2,2})/([a-zA-Z0-9_\-]+)$ index.php?w=$1&h=$2 [QSA]
PS. I hope the above URL /12/3 is just for example because your regex accepts only a-z
RewriteEngine On
RewriteRule ^[a-z]{2,2})/([a-zA-Z0-9_-]+)$ /index.php?w=$1&h=$2 [QSA]
Essentially, I'm pretty sure you need a / in front of index.php
I like to use this generator for my mod-rewrite rules.
http://www.generateit.net/mod-rewrite/
Try changing your rewriterule to the following:
RewriteEngine on
RewriteRule ^([a-z0-9]{2})/([a-zA-Z0-9_-]+)$ /index.php?w=$1&h=$2 [QSA]
Your example has two digits as the first parts of the path, which won't be matched by [a-z].
RewriteEngine On
RewriteRule ^(.*)/(.*)$ index.php?w=$1&h=$2 [QSA]
The above sorted it. Thanks for the replies
I have the following mod_rewrite rule:
RewriteRule ^([^/.]+)/?$ search.php?action=procedure&procedureName=$1
This works fine in redirecting things like /blabla to /search.php?action=procedure&procedureName=blabla
The problem is that sometimes I want to pass a 'start' value (for pagination). For example, /blabla/?start=20.
Currently, it just ignores it. Printing out the $_REQUEST array doesn't show 'start'. I tried modifying the rule to:
RewriteRule ^([^/.]+)/\?start=([0-9]+)$ search.php?action=procedure&procedureName=$1&start=$2
RewriteRule ^([^/.]+)/?$ search.php?action=procedure&procedureName=$1
But it didn't do anything.
Any idea?
Thanks
RewriteRule ^([^/.]+)/?$ search.php?action=procedure&procedureName=$1 [L,NC,QSA]
The QSA means query string append, and it'll append $_GET vars you pass. Otherwise, they are normally not added.
RewriteRule applies to the path. You need to use RewriteCond to match the query string.
http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html#rewriterule
Or to just pass the query string through, use %{QUERY_STRING}
RewriteRule ^([^/.]+)/?$ search.php?action=procedure&procedureName=$1&%{QUERY_STRING}
Actually I think I found my answer:
RewriteRule ^([^/.]+)/?$ search.php?action=procedure&procedureName=$1 [QSA]
The QSA allows it to pass query strings.
Right?