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
Related
I'm building a RestApi and I have a problem with urls. I want to transform "api.php?request=users" into "api/users". But I also want it to be dinamic. For example: If a user types "api/users/13" I want it to be tha same as "api.php?request=users&id=13". Is there any way to make it dinamic? Do I have to create every case individually? How could this be done in each of these two cases? Thanks so much in advance for your help!!
the best way is change .htaccess file like this:
RewriteEngine On
RewriteRule ^api/([^/])/([^/])/([^/]*)$ /api.php?request=$1&id=$2&state=$3 [L]
The original URL:
http://www.domain.com/api.php?request=mike&id=3&state=active
The rewritten URL:
http://www.domain.com/api/mike/3/active
update: if you don't know the number of parameter add one more rewrite for each 'level':
RewriteRule ^api/([^/])/([^/])/([^/])$ /api.php?request=$1 [L]
RewriteRule ^api/([^/])/([^/])/([^/])$ /api.php?request=$1&id=$2 [L]
RewriteRule ^api/([^/])/([^/])/([^/]*)$
/api.php?request=$1&id=$2&status=$3 [L]
Regards.
I think you must add a new RewriteRule, because you need different variables.
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^(.*)/(.*)/?$ $1.php?request=$2
RewriteRule ^(.*)/(.*)/(.*)/?$ $1.php?request=$2&id=$3
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'm just wondering how other websites replace there get variable.
example:
http://www.example.com/page.php?page=1
replaced:
http://www.example.com/page/1
indeed, just what ssx said.
You need a .htaccess file in the root of the website, with some content that looks like this (i use this one)
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([\(\)\|a-zA-Z0-9_-]+)/([0-9]+)/?$ /index.php?ext=$1&cat=$2&name=$3&urlID=$4 [L]
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([\(\)\|a-zA-Z0-9_-]+)/?$ /index.php?ext=$1&cat=$2&name=$3 [L]
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/p=([0-9]+)/?$ /index.php?ext=$1&cat=$2&p=$3 [L]
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/?$ /index.php?ext=$1&cat=$2 [L]
RewriteRule ^([a-zA-Z0-9_-]+)/?$ /index.php?ext=$1 [L]
I'd say take a look at an answer I provided someone else a while back. I'd type it all out here again or copy and paste it but the link is much easier.
PHP dynamic DB page rewrite URL
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