Mod_rewrite handling extra get variables (QSA not working) - php

I have a mod rewrite rule for settings page: (localhost/settings/index.php)
RewriteRule settings/([a-zA-Z0-9_]+)/?$ settings/?path=$1 [QSA,L]
And I am handling menu references within the URL which will be similar to:
http://localhost/settings/xyz/?ref=menu1&abc=2&de=3..
However, with my current rewrite rule, the variable does not get passed along and I didn't get the values of ref,abc..
I read here about QSA flag but that doesn't seem to be working.
What am I doing wrong?

Try turning off MultiViews option:
Options -MultiViews
RewriteEngine On
RewriteRule ^settings/([\w-]+)/?$ settings/?path=$1 [QSA,L,NC]
In place of QSA, you can use this trick to captured query string:
RewriteRule ^settings/([\w-]+)/?$ settings/?path=$1&%{QUERY_STRING} [L,NC]

Related

Optional Parameter in mod_rewrite (pretty url)

I am trying to do the following:
Access user.php?lang=$1&id=$2&title=$3
via
en/user/1/tyler (for English language)
and
user/1/tyler (without language parameter)
My rules are:
RewriteRule ^(en)(?:/(user)/([0-9]+)/([a-zA-Z0-9-]*))?/?$ user.php?lang=$1&id=$2&title=$3 [L,QSA]
RewriteRule ^(user)/([0-9]+)/([a-zA-Z0-9-]*)?$ user.php?lang=&id=$1&title=$2 [L,QSA]
It works when I put the language (en) on the front and I can retrieve all the parameters via $_GET but it doesn't work without it.
How could I achieve this?
Thank you
You may try these rules:
Options -MultiViews
RewriteEngine On
RewriteRule ^(en)/user/(\d+)/([\w-]+)/?$ user.php?lang=$1&id=$2&title=$3 [L,NC,QSA]
RewriteRule ^user/(\d+)/([\w-]+)/?$ user.php?lang=&id=$1&title=$1 [L,NC,QSA]
It is important to turn off MultiViews to avoid using content negotiation service in Apache that may override mod_rewrite rules.

HTACCESS friendly url and allow GET method

My code is
Options -Multiviews
RewriteEngine On
RewriteBase /
RewriteRule ^([a-z0-9-]+)\.html$ /index.php?cat=$1 [L]
If I access
mysite.com/name-of-category.html
it works, but if I access
mysite.com/name-of-category.html?anything=something
it shows the webpage but $_GET["anything"] shows nothing.
You must specify an option called QSA or 'Query String Append':
RewriteRule ^([a-z0-9-]+)\.html$ /index.php?cat=$1 [L,QSA]
It will ensure that the original query strings are also included as part of your new URL.

.htaccess url rewrite having issues

I am not able to customize a url using .htaccess. I applied all changes in .htaccess but this one stuck with me for long time.
I have a URL :
product.php?act=pro&cat=1&category=Nokia
I want to change it to:
product/1/Nokia.html
I used following rule:
RewriteRule ^product/?([a-zA-Z_]+)/([a-zA-Z_]+)\.html$ product.php?act=$1&cat=$2&category=$3 [QSA]
But not getting this one correct.
You're capturing only 2 groups and using $1, $2, $3.
Change your code to this:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule ^product/([^/]+)/([^.]+)\.html$ /product.php?act=pro&cat=$1&category=$2 [QSA,L,NC]
Try this:
RewriteRule ^/product/([0-9]+)/(.*).html$ ^/product.php?act=pro&cat=$1&category=$2 [NC,L]
Pattern that matches cat should have 0-9 in it

.htaccess redirect normal url to query string

I have a url of the form:
www.example.com/r/abc/1234
that I want to rewrite to:
www.example.com/r.php?deb=abc&id=1234
And another url:
www.example.com/r/12bcd-qsqs-343-wdwd/1234
which should be rewritten to:
www.example.com/r.php?abc_id=12bcd-qsqs-343-wdwd&id=1234
Both of these rule should be handled by .htaccess. Any suggestions?
Both the URIs that you want to redirect look similar except that 2nd one has hyphens - in first query parameter.
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule ^r/([^-]+)/(.+)/?$ r.php?deb=$1&id=$2 [L,QSA]
RewriteRule ^r/([a-z0-9-]+)/(.+)/?$ r.php?abc_id=$1&id=$2 [L,QSA,NC]
Try something like :
RewriteRule (.*)/(.*)/(.*)$ path_to_php_files/$1.php?deb=$2&id=$3 [QSA,L,NC]
Unfortunately with this rule, you would have the first $_GET parameter always deb and I hope you have the DirectoryIndex set up corectly. I do not think that for
www.example.com/r/abc/1234
www.example.com/r/12bcd-qsqs-343-wdwd/1234
you can delivrer the abc and 12bcd-qsqs-343-wdwd to 2 different $_GET parameters if you have dynamic links.

Using Mod Rewrite to rewrite a url?

I have the following format:
http://www.mydomain.com/view/product/ID/CAT/TITLE
ID and CAT are both numeric. So, some real examples:
http://www.mydomain.com/view/product/1/2/ipod
http://www.mydomain.com/view/product/3/4/40-inch-tv
etc
I want to know if I should try to use mod rewrite, and how, or if I should rather fix the PHP to handle the paths correctly. I want to map the above to something like:
http://www.mydomain.com/CATEGORY/SUB_CATEGORY/TITLE
Any ideas?
RewriteEngine on
RewriteBase /
RewriteRule ^([A-Za-z0-9]+)$ /$1/ [R]
RewriteRule ^([A-Za-z0-9]+)$ index.php?cat=$1
RewriteRule ^([A-Za-z0-9]+)/([A-Za-z0-9]+)$ /$1/$2/ [R]
RewriteRule ^([A-Za-z0-9]+)/([A-Za-z0-9]+)$ index.php?cat=$1&subcat=$2
RewriteRule ^([A-Za-z0-9]+)/([A-Za-z0-9]+)/([A-Za-z0-9]+)$ /$1/$2/$3/ [R]
RewriteRule ^([A-Za-z0-9]+)/([A-Za-z0-9]+)/([A-Za-z0-9]+)$ index.php?cat=$1&subcat=$2&title=$3
in php you can get the variables as you would normally do, also you will need a rule for all variables entered and/or only category or sub-category or you will end up with a 404 when someone tries to access those pages
I would say that your question is not totally clear to me. Going by your examples what do you want your target URL to become from http://www.mydomain.com/view/product/1/2/ipod.
Will it be: http://www.mydomain.com/2/1/ipod ?
If yes then enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule ^view/product/([0-9]+)/([0-9]+)/(.*)$ $2/$1/$3 [L,R,NC]

Categories