mod rewrite problem with 2 parameters - php

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]

Related

About RewriteRule and .htaccess

I have a problem:
I want to rewrite this:
example.com/pages/?page=start
or
example.com/pages/?page=news
to:
example.com/start
or
example.com/news
So I want the GET from the subpage to be displayed as a main page, does anyone know how?
If you want that the user enter something like example.com/start or example.com/news but the server should process it like example.com/pages/?page=start or example.com/pages/?page=news this is done as follows:
RewriteEngine On
RewriteRule ^/?([^/]+) /pages/?page=$1 [QSA,L]
Be careful here because example.com/news/first-news will also be rewritten to example.com/pages/?page=news and /first-news will be lost!

How to merge 2 rewrite rule using Apache mod_rewrite module in php

I was wondering if i could merge 2 rewrite rules. I'm using the Apache mod_rewrite module.
rule #1
RewriteRule ^albums/singles/?$ discography/discography.php?primarytype=2 [L,QSA]
rule #2
RewriteRule ^albums/ep/?$ discography/discography.php?primarytype=3 [L,QSA]
Can i merge those 2 rules ?
RewriteRule ^albums/(singles|ep)/?$ discography/discography.php?primarytype=2 [L,QSA]
See if this works for you? I have not tested it right now.
The first part is the regex the second is the url that is to be rewritten to. So in second url i think this (2|3) might not work.
Actually you can capture the dynamic part of the url in RewriteCond and then use that as primarytype=$1
That is your result url can become primarytype=ep or primarytype=singles but it does not seem like they can be made 2 or 3 dynamically.
RewriteRule ^albums/(singles|ep)/?$ discography/discography.php?primarytype=$1 [L,QSA]
The above might be an option for you to generate the urls with primarytype=ep or primarytype=singles but it does not seem like 2 or 3 can be dynamically chosen like you have mentioned in the question.

Rewrite dynamic url in .htaccess?

Here is an example url.
97006 is the query string.
Customer-Manager is a title that changes for every page.
jobdetails.php is run when a button is clicked which brings you to a page like this below.
http://11.11.111.111/97006/Customer-Manager.html
I would like the url to be:
http://11.11.111.111/job/Customer-Manager.html
In the .htaccess there is already code for the url I want to change.
I think this is the condition for the rule: (because its the 1st condition & 1st rule, is thst how it works?)
RewriteCond %{QUERY_STRING} base64_encode.*\(.*\) [OR]
This is the rule I have tried changing many times but no success:
RewriteRule ^([1-9][0-9]*)/([-.,_0-9a-zA-z]*)\.html$ job_details.php?query_string=$1&action=%1 [L]
This code makes the url when for the button:
$button = tep_href_link($id.'/'.$title.'.html');
I have research apache directives, regular expressions and tried many tutorials but whenever I change anything in the htaccess for that line I get sent to the wrong url or the site crashes altogether.
I think it is impossible you can use this url instead :
http://example.com/jobs/97006/data.html
RewriteEngine On
RewriteRule ^jobs/([^/]*)/([^/]*)\.html$ /job_details.php?query_string=$1&action=$2 [L]

URL Rewrite is not working properly

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.

.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.

Categories