GET parameters in htaccess url rewriting - .htaccess

I'm doing some url rewrite with htaccess. I have this:
RewriteRule ^mesas$ http://www.myweb.net/piezas.php?pos=1 [L]
RewriteRule ^sillas$ http://www.myweb.net/piezas.php?pos=2 [L]
RewriteRule ^taburetes$ http://www.myweb.net/piezas.php?pos=3 [L]
This is working. If I write the url http://www.myweb.net/mesas
I get http://www.myweb.net/piezas.php?pos=1.
Now I want to add other parameters. Example:
I have this http://www.myweb.net/mesas?pag=10
and I want this: http://www.myweb.net/piezas.php?pos=1&pag=10
I'm try with:
RewriteRule ^mesas?(.*)$ http://www.myweb.net/piezas.php?pos=1&$1 [L]
and
RewriteRule ^mesas$?(.*) http://www.myweb.net/piezas.php?pos=1&$1 [L]
But it doesn't work. What am I doing wrong?
Thanks!

Try adding flag QSA, like this:
RewriteRule ^mesas$ http://www.myweb.net/piezas.php?pos=1 [L,QSA]
RewriteRule ^sillas$ http://www.myweb.net/piezas.php?pos=2 [L,QSA]
RewriteRule ^taburetes$ http://www.myweb.net/piezas.php?pos=3 [L,QSA]
It will append the incoming query string to the existing one.

Related

Redirect URL with GET variable to site with the GET variable in it

I would like to redirect, for example :
index.php?name=sarah
to
names/sarah.php
The folder structure:
names/...
index.php
I've tried :
RewriteRule index.php?name=$1 /names/$1.php
and other ones, but didn't came to a solution.
Is this possible? If so, how can you do this?
Note:
The other way round is
RewriteRule ^names/([^/]*)\.php$ index.php?q=$1 [QSA,L]
which works fine.
Here is your rule:
RewriteCond %{QUERY_STRING} name=([^&]*)
RewriteRule ^.*$ /names/%1.php?
You first example wont work as you are trying to manipulate query strings using a RewriteRule but this is not allowed. You can try the following :
RewriteEngine on
RewriteCond %{THE_REQUEST} / index\.php\?q=([^\s]+) [NC]
RewriteRule ^ /names/%1.php? [L,R]
RewriteRule ^names/([^/]*)\.php$ index.php?q=$1 [QSA,L]

Url rewrite and trim url

I need your precious help.
I have an url:
http://domain.com/v/12345
My ht access is set to pass 12345 to php script like:
htaccess:
RewriteRule ^v/([a-zA-Z0-9_-]+)$ /script.php?v=$1 [L]
script php:
http://domain.com/script.php?code=12345
and works perfectly but.. and there is a but, I would write my url so:
http://domain.com/12345/my-dog-is-very-fat
can you help to change my htaccess to works so? Because if I use /my-dog-is-fat or just / I get an 404 error.
I try to explain better:
I would share link with seo keywords in the url, example
mydomain.com/alphanumericCode/thi-is-my-dog-article
where alphanumericCode is get of myscript.php?V=
This is my htaccess:
RewriteEngine On
RewriteRule ^view/([a-zA-Z0-9_-]+)$ /view.php?v=$1
RewriteRule ^folder/([a-zA-Z0-9_-]+)$ /filefolderlist.php?f=$1
RewriteRule ^home$ /index.php [L]
RewriteRule ^report$ /report.php [L]
RewriteRule ^play/([a-zA-Z0-9_-]+)$ /play.php?v=$1 [L]
###Added by anubhava
RewriteRule ^v/([\w-]+)/?$ /script.php?v=$1 [L,QSA]
RewriteRule ^([0-9a-z]+)/.+$ /script.php?v=$1 [L,QSA,NC]
##end
RewriteRule ^privacy-policy$ /privacy.php [L]
ErrorDocument 404 404.php
You can have a new rule for the new URI scheme:
RewriteEngine On
RewriteRule ^v/([\w-]+)/?$ /script.php?v=$1 [L,QSA]
RewriteRule ^([0-9a-z]+)/.+$ /script.php?v=$1 [L,QSA,NC]

Rewrite url with .htacess doesnt work

I have a link
http://www.mydomain.com/place/index.php?slug=antalya-hotels
and i want to write this like
http://www.mydomain.com/place/antalya-hotels.html
I have tried this
RewriteEngine On
RewriteRule ^([^/]*)\.html$ /place/index.php?slug=$1 [L]
Bu didnt work. how we can do.
You can use:
RewriteEngine On
RewriteRule ^place/([^.]+)\.html$ /place/index.php?slug=$1 [L,QSA,NC]
OR
RewriteRule ^([^/]+)/([^.]+)\.html$ /$1/index.php?slug=$2 [L,QSA,NC]

Htaccess url rewrite not working correctly

I have an issue with url rewriting, here's what I have so far:
Options +FollowSymlinks
RewriteEngine on
RewriteRule \.(gif|png|jpg|css|js)$|^index\.php$ - [L]
RewriteRule ^users/(.+)$ /profile.php?userid=$1 [NC]
RewriteRule ^(.+)/(.+)$ /main.php?region=$1&place=$2 [NC]
Basically, if the url is mysite.com/users/username I want it to use profile.php, then for anything else, to go to main.php.
However, with that code it doesn't happen as expected, it always uses main.php. Is there anyway you can use if and elses in htaccess, or something similar?
You're missing L flag from 2nd and 3rd rules. Have your code like this:
RewriteRule \.(gif|png|jpg|css|js)$|^index\.php$ - [L]
RewriteRule ^users/(.+)$ /profile.php?userid=$1 [NC,L,QSA]
RewriteRule ^(.+)/(.+)$ /main.php?region=$1&place=$2 [NC,L,QSA]

URL Rewriting Help

I'm trying to learn Regex & URL Rewriting in PHP. How can I create an .htaccess file which will rewrite (not redirect) any url to index.php?q={url}?
For example:
http://www.example.com/baloon
to
http://www.example.com/index.php?q=baloon
I tried:
RewriteEngine On
RewriteRule ^$ index.php?q=$1 [R]
...but it is not working. What am I doing wrong?
Thanks.
Rewriting ANY url to index.php?q=$1 would result in internal server error as it'd create an endless loop; instead do something like this:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/index\.php
RewriteRule ^(.*)$ index.php?q=$1 [L]

Resources