URL Rewrite is not working properly - php

This is my URL rewrite:
RewriteRule ^cars/$ cars.php
RewriteRule ^cars/([a-z-]+)/$ /cars.php?model=$1
This works, so my URL is like this:
example.com/cars/porche/
refers to
cars.php?model=porche
Now Im making more search criterias, so I want to be able to add for example model year, car manufactor, etc. like this:
example.com/cars/porche/?model_year=xxx&car_manufactor=xxx
Right now this does not work with the current rewrite, but I can't figure out why.

Simple, just add QSA:
RewriteRule ^cars/([a-z-]+)/$ /cars.php?model=$1 [QSA]
That tells the rewrite engine to append any other query parameters to the rewritten URL as well.

Related

Rewrite urls php/mysql page with htaccess apache

I have a search form to search for a health institution in a city.
when I display the results I have this URL for example :
search.php?city=mycity&speciality=cardiology
I would like to rewrite this URL like this: health-institution-cardiology-mycity
I set up an .htaccess rule like this :
RewriteEngine on
RewriteRule health-entity-([a-zA-Z\-]+)-([a-zA-Z\-]+) search.php?city=$2&speciality=$1
It doesn't work, however, I applied this rule to another type of URL and it works
is there any issue with the code?
do I need to add something?
This will work:
RewriteEngine on
RewriteRule ^health-institution-([a-z]+)-([a-z]+)/?$ search.php?city=$2&speciality=$1 [NC,L]
Note: The NC (No Case) at the end makes the rurl case insensitive.

htaccess rewrite rule for url varible as parent folder

I already create a .htaccess in my folder and would like to make the URL:
www.example/good/page
toward:
www.example/page?type=good
my current document is written as:
RewriteRule ^(\w+)$\/page page.php?type=$1 [NC,L]
but it didn't work, and I don't know how to check it is correct or not
would anyone able to provide some example for that?
thanks!
Make sure your rewite engine is on, and it is rewriting the base. Change your code like the following:
RewriteEngine on
RewriteBase /
RewriteRule ^/?([^/]+)/page?$ "page.php?type=$1" [L,QSA]
And then your rewrite rule must look like this.
The RewriteRule basically means that if the request is done that matches ^/?([^/]+)/page?$ (matches any URL except the server root), it will be rewritten as page.php?type=$1 which means a request for page.php be rewritten as page.php?type=good).
QSA means that if there's a query string passed with the original URL, it will be appended to the rewrite (example.com/good/page?id=2 will be rewritten as page.php?type=good&id=2.
L means if the rule matches, don't process any more RewriteRules below this one.
Try this and let me know. Accept the answer if it worked for you.

Custom url rewrite help needed

I have created a database with url slug and it contains urls like mysite.com/blog/content/user1 and blog is folder where file is so I want to change mysite.com/blog/page.php?name=content&user=user1 to mysite.com/blog/content/user1 how can I do that I have tried
RewriteEngine on
RewriteRule ^([a-z]+)$ page.php?name=$1&user=$2 but that doesn't works
Try this one
RewriteEngine on
RewriteBase /
RewriteRule ^blog/([^//]+)/([^//]+)$ blog/page.php?name=$1&user=$2 [QSA,L]
Please avoid making the slug record using / which means the uri segment better to use - for you slug so if your url is something mysite.com/blog/page.php?name=content&user=user-a and you want to show like mysite.com/blog/content/user-a them above rule will also work for that url aso

.htaccess with pagination in php?

I am currently coding a pagination script into many parts of my site, this has been a well needed and requested feature and I have finally been able to come round and start coding it, it is all going well, until I find that my rewritten urls don't like working with the pagination urls.
So, an example page on my site would be news.php. This file structure can be something like news.php?id=5. I have rewritten the url like so:
/news/5/
## Rewrite URL's for News & Dev ##
RewriteRule ^news/([0-9]+)/$ /news.php?id=$1 [L]
RewriteRule ^news/([0-9]+)$ /news.php?id=$1 [L]
RewriteRule ^news$ /news.php [L]
RewriteRule ^news/$ /news.php [L]
The pagination script I am using prepends two new variables in the url, the new url turns out to be this:
news.php?page=1&ipp=55id=5
I would really appreciate it if anyone could assist me in making the urls look better, as it defeats the object of having it in the first place if after they use the pagination, it changes the url back to a clunky and ugly url.
I don't want it to be required to have parts of the url, that is something I really don't want..
e.g I don't want the url to be required to be /news/1/55/5, instead id like it to be optional.
Thank you for your time, it is appreciated!
Additional Information
The links in my news script currently display like so:
news.php?page=1&ipp=55id=5
I don't like to see ugly urls like that, and want to make the url look better using mod_rewrite, It would be better if the urls would display like so:
/news/PAGE/IPP/ID/ -> return news.php?page=1&ipp=55id=5
Also, to make it as user friendly as possible, I don't want any of the fields to be required as such, so for example I would like to have the following link accessible at all times without it requiring the other fields.
/news/ID/
Then, when the user clicks a pagination link, it would use the following link structure:
/news/PAGE/IPP/ID/ -> return news.php?page=1&ipp=55id=5
This is all from user feedback of my site, and is something that people have been asking for. Problem is, I don't understand even simple .htaccess
Thanks
RewriteBase /
# add slash to end of url if not present (and do a redirect)
RewriteCond $0 !/$
RewriteRule ^news([^\.]*)$ $0/ [L,R=302]
# rewrite url with format /news/[<id>/[<page>/[<ipp>/]]]
RewriteRule ^news/(?:([0-9]+)/)?(?:([0-9]+)/)?(?:([0-9]+)/)?$ /news.php?id=$1&page=$2&ipp=$3 [L]
Not sure what ipp is supposed to be, but my guess is it shows the number of item per page. I would personally not like to have that in my url.
You can have :
news/id/page/ipp with
RewriteRule ^news(/?)$ news.php [L]
RewriteRule ^news/([0-9]+)-(.*)_([0-9]+)(/?)$ news.php?page=$1&ipp=$2&id=$3 [L]
news/1222-subjet-for-example_34
return :
news.php?page=1222&ipp=subject-for-example&id=34
use (/?) instead of create many rules ;)
Hope it's works for you.

mod rewrite problem with 2 parameters

I'm trying to rewrite the categoy file of my shop system, I also integrated a pagination so I need to rewrite 2 parameters. it almost works, otherwise I wouldn't be posting in here
this is the rewriteurl
RewriteRule ^shop/cat/(.*)/page/([0-9]+)$ /cmstut/shop/cat.php?cat=$1&page=$2 [QSA,L]
This is the url
http://localhost/cmstut/shop/cat/32/page/2
the cat works but not the page and when I print the querystring I get this:
cat=32/page/2
What did I do wrong? I was expecting something like cat=32&page=2 so I could catch the page and show the right page for the pagination.
You’re probably having two rules where the second one looks like this:
RewriteRule ^shop/cat/(.*)$ /cmstut/shop/cat.php?cat=$1 [QSA,L]
This rule will cause that a request of shop/cat/32/page/2 will be rewritten wrong. You need to use a more specific pattern like this:
RewriteRule ^shop/cat/([^/]+)$ shop/cat.php?cat=$1 [QSA,L]
RewriteRule ^shop/cat/([^/]+)/page/([0-9]+)$ shop/cat.php?cat=$1&page=$2 [QSA,L]

Categories