PHP/Apache rewrite rules with multiple parameters? - php

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.

Related

How to rewrite specific URL using htaccess

I've been breaking my head on this for quite some time and I don't see the solution.
I want to rewrite a URL with a GET language parameter to a more clean URL.
For instance:
http://www.example.com?lang=en
Needs to be:
http://www.example.com/en
The above works fine with this rewrite rule:
RewriteRule ^(en|nl|fr|de)/?$ /?lang=$1 [L]
But I can't get it to work on URLs like these:
http://www.example.com/contact.php?lang=en
http://www.example.com/about.php?lang=en
That need to be:
http://www.example.com/en/contact.php
http://www.example.com/en/about.php
Anyone have an idea what I'm missing in my rewrite rule to make this work?
You will need an additional rewrite rule for handling /en/about.php:
RewriteEngine On
RewriteRule ^(en|nl|fr|de)/([\w-]+\.php)$ $2?lang=$1 [L,QSA]
RewriteRule ^(en|nl|fr|de)/?$ /?lang=$1 [L,QSA]

.htaccess restricting $_GET

I have a project for modification, where I need to post a form with get, but I am unable to get the post value.
after checking I determine that .htaccess causing problem for this, please help me on this so I get the value without affecting the existing functionality of site.
this is .htaccess file
RewriteRule ^/?([a-zA-Z0-9_+-\s+]+)$ ?x=$1 [QSA,L]
RewriteRule ^/?([a-zA-Z0-9_+-\s+]+)/([a-zA-Z0-9_+-\s+]+)$ ?x=$1&y=$2
RewriteRule ^/?([a-zA-Z0-9_+-\s+]+)/([a-zA-Z0-9_+-\s+]+)/([a-zA-Z0-9_+-\s+]+)$ ? x=$1&y=$2&z=$3
RewriteRule ^/?([a-zA-Z0-9_+-\s+]+)/([a-zA-Z0-9_+-\s+]+)/([a-zA-Z0-9_+-\s+]+)/([a-zA-Z0-9_+-\s+]+)$ ?x=$1&y=$2&z=$3&u=$4
RewriteRule ^/?([a-zA-Z0-9_+-\s+]+)/([a-zA-Z0-9_+-\s+]+)/([a-zA-Z0-9_+-\s+]+)/([a-zA-Z0-9_+-\s+]+)/([a-zA-Z0-9_+-\s+]+)$ ?x=$1&y=$2&z=$3&u=$4&v=$5
RewriteRule ^/?([a-zA-Z0-9_+-\s+]+)/([a-zA-Z0-9_+-\s+]+)/([a-zA-Z0-9_+-\s+]+)/([a-zA-Z0-9_+-\s+]+)/([a-zA-Z0-9_+-\s+]+)/([a-zA-Z0-9_+-\s+]+)$ ?x=$1&y=$2&z=$3&u=$4&v=$5&w=$6
RewriteRule ^/?([a-zA-Z0-9_+-\s+]+)/([a-zA-Z0-9_+-\s+]+)/([a-zA-Z0-9_+-\s+]+)/([a-zA-Z0-9_+-\s+]+)/([a-zA-Z0-9_+-\s+]+)/([a-zA-Z0-9_+-\s+]+)/([a-zA-Z0-9_+-\s+]+)$ ?x=$1&y=$2&z=$3&u=$4&v=$5&w=$6&a=$7
RewriteRule ^/?([a-zA-Z0-9_+-\s+]+)/([a-zA-Z0-9_+-\s+]+)/([a-zA-Z0-9_+-\s+]+)/([a-zA-Z0-9_+-\s+]+)/([a-zA-Z0-9_+-\s+]+)/([a-zA-Z0-9_+-\s+]+)/([a-zA-Z0-9_+-\s+]+)/([a-zA-Z0-9_+-\s+]+)$ ?x=$1&y=$2&z=$3&u=$4&v=$5&w=$6&a=$7&b=$8
eg : http://www.exampledomain.com/result?term=sampleterm
in this the $_GET['x'] value is result, but I am unable to get term value
You actually need QSA flag (Query String Append), and its better to use L (Last) flag also.
RewriteRule ^/?([\w\s+-]+)/?$ ?x=$1 [QSA,L]
RewriteRule ^/?([\w\s+-]+)/([\w\s+-]+)/?$ ?x=$1&y=$2 [L,QSA]
RewriteRule ^/?([\w\s+-]+)/([\w\s+-]+)/([\w\s+-)/?$ ?x=$1&y=$2&z=$3 [L,QSA]
RewriteRule ^/?([\w\s+-]+)/([\w\s+-]+)/([\w\s+-]+)/([\w\s+-]+)/?$ ?x=$1&y=$2&z=$3&u=$4 [L,QSA]
# rest of the rules here...
QSA flag makes sure to append existing query string in the new query parameter you're adding through your rules.

how to retrieve a specific get data in htaccess and pass it on rewrite rule

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.

extending a htaccess rewrite rule

I have following rewrite rule/condition in my htaccess:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^id([0-9]+)/?$ /profile.php?user_id=$1 [L]
The result is "example.com/id1"
Now the problem I have, I need to put a $_GET parameter sometimes to the URL, but actually they get ignored, example:
example.com/id1?section=video
example.com/id1?section=photo
example.com/id1?section=audio
Also not working:
example.com/id1&section=video
Is there a chance to accomplish this task by extending the htaccess rule? Remember that the URL not always have the $_GET parameter added.
Best regards!
Try using the QSA flag
RewriteRule ^id([0-9]+)/?$ /profile.php?user_id=$1 [L,NC,QSA]

Retain query string on rewrite .htaccess

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

Categories