I am new to Mod Rewrite, i just learnt a little bit so i tried to write different rules for each get parameter. Here is my full URL:
localhost/index.php?view=some-page&city=some-city&ward=some-ward&price=some-price&page=some-page
As you can see, there are 5 get parameters and the last four (city, ward, price, page) are optional. So i write different rules for each optional parameter like this:
RewriteEngine On
RewriteBase /
# No 4 optional parameter
RewriteRule ^([^/]*)/?$ index.php?view=$1 [QSA,NC,L]
# for set city parameter
RewriteRule ^([^/]*)/([^/]*)\.html$ /index.php?view=$1&city=$2 [NC,L]
# for set price parameter
RewriteRule ^([^/]*)/price/([0-9]+)\.html$ /index.php?view=$1&price=$2 [NC,L]
#for set page parameter
RewriteRule ^([^/]*)/page/([0-9]+)\.html$ /index.php?view=$1&page=$2 [NC,L]
# for set city and page parameter
RewriteRule ^([^/]*)/city/([^/]*)/page/([0-9]+)\.html$ /index.php?view=$1&city=$2&page=$3 [NC,L]
# for set city, ward and page
RewriteRule ^([^/]*)/city/([^/]*)/ward/([^/]*)/page/([0-9]+)\.html$ /index.php?view=$1&city=$2&ward=$3&page=$4 [NC,L]
# all city, ward, price and page are set.
RewriteRule ^([^/]*)/city/([^/]*)/ward/([^/]*)/price/([^/]*)/page/([0-9]+)\.html$ /index.php?view=$1&city=$2&ward=$3&price=$4&page=$5 [NC,L]
This is such a headache by looking at this and i don't know whether what i did is effecient or not. So What i ask here is that: instead of writting different rule for each case, can we just write one line of Rewriterule for all like this:
RewriteRule ^([^/]*)/city/([^/]*)/ward/([^/]*)/price/([^/]*)/page/([0-9]+)\.html$ /index.php?view=$1&city=$2&ward=$3&price=$4&page=$5 [NC,L]
And telling that rule if parameter city is not set (empty), then the mapping url should be without city like this:
/index.php?view=$1&&ward=$2&price=$3&page=$4
Many thanks in advance.
Related
i am working on project, which is running XAMPP localhost and PHP MYSQLI,
my question : how i replace "?","=" signs with "/" slash. ?
like, my url is "archive?date=2017-06-02&p=4"
and i want to force it "archive/2017-08-02/4"
i found many codes on stackoverflow and some other sites, but that are not working for me.
if codes are working then, CSS files and GET method doesn't work on my project.
complete code of .htaccess is given below.
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^([^=]*)=([^=]*)=(.*) /$1/$2/$3 [N]
RewriteRule ^([^=]*)=([^=]*)$ $1/$2 [L]
RewriteRule ^home index.php [NC,L]
RewriteRule ^archive archive.php [NC,L]
RewriteRule ^about about.php [NC,L]
RewriteRule ^article article.php [NC,L]
RewriteRule ^news news.php [NC,L]
RewriteRule ^video videos.php [NC,L]
RewriteRule ^video?vid=([0-9]+) videos.php?q=$1 [NC,L]
RewriteRule ^article?num=([0-9]+) article.php?num=$1 [NC,L]
RewriteRule ^editorial?num=([0-9]+) editorial.php?num=$1 [NC,L]
RewriteRule ^news?news=([0-9]+) news.php?news=$1 [NC,L]
You cannot check against the query string in a rewrite rule. You need rewrite conditions for that:
RewriteCond %{QUERY_STRING} date=([^&]+)&p=(.+)
RewriteRule ^archive/? /archive/%1/%2?
Demo here: http://htaccess.mwl.be?share=81e85c09-d505-5206-ab14-6c5059107808
If you want to actually redirect just add [R=301,L] to the end of the RewriteRule.
However, looking at the above I suspect you have your script sitting listening at /archive/index.php?data=foo&p=bar but want URLs to be like /archive/date/p, ie pretty.
This is actually a very common misconception about how htaccess URL rewrites work when you first get into them.
RewriteRules will mask or redirect URLs for you but they cannot change the underlying location a script is located at and thus the address used to pass it information.
In other words - you can mask /archive/index.php?data=foo&p=bar as /archive/date/p so that requests made to /archive/date/p resolve to /archive/index.php?data=foo&p=bar, but you cannot make it so that if you enter /archive/index.php?data=foo&p=bar as URL you have the URL change to /archive/date/p while still serving content from /archive/date/p. It has to be either or.
If this all sounds about right my advice would be as follows:
First, put your code into a different file, say /archive/script.php.
Next add the following to your htaccess:
RewriteCond %{QUERY_STRING} date=([^&]+)&p=(.+)
RewriteRule ^archive/? /archive/%1/%2? [R=301,L]
RewriteRule ^archive/([^/]+)/([^/]+) /archive/script.php?date=$1&p=$2
Note that the first two lines are the same as before, but now there is a new line that looks for the masked URL format of /archive/date/p and sends it off to the actual script, which is handled by the new RewriteRule.
The behaviour of the new rule is demoed here: http://htaccess.mwl.be?share=06751667-f16f-5c13-91eb-dd5cffdc6db3
Hope this makes sense / helps.
how can first category page (page/1) masking on htaccess. i need one line writing this code?
RewriteRule ^animal$ categorie.php?cID=1&page=1 [L]
RewriteRule ^animal/page/([0-9]+)$ categorie.php?cID=1&page=1$ [L]
my another category like this:
www.domain.com/women = domain.com?catID=2&page=1
www.domain.com/women/page/2 = domain.com?catID=2&page=2
i have 11 categories
sorry for english.
Unfortunately, if you're going to translate a category name (animal, women,...) into an ID number, that's going to have to be done case by case. Think about passing the category name (catName=$1) instead of a number cId=nnn. Then you would set the cId inside your PHP code.
If no page is given, you would need a separate RewriteRule to handle a default of page 1.
RewriteEngine On
RewriteRule ^([^/]+)/?$ /category.php?catName=$1&page=1 [QSA]
RewriteRule ^([^/]+)/page/([0-9]+)/?$ /category.php?catName=$1&page=$2 [QSA]
is one way to do it.
Now, in /category.php, you have $_GET['catName'] that you can look up the $catID for, and $_GET['page'] should be set. To be safe, check that they are set with isset(), and perhaps check for valid values.
i solved like this.
RewriteRule ^(animal|women|live|football)$ /categorie.php?cat=$1&page=1 [L]
RewriteRule ^(animal|women|live|football)/page/([0-9]+)$ /categorie.php?cat=$1&page=$2 [L]
thanks
On my website I have categories with urls like so
www.example.com/?foo and www.example.com/?bar
In these categories I have articles with urls like this:
www.example.com/?foo&nr=12
and groups with urls like this:
www.example.com/?foo&group=somegroup
I am trying to rewrite
/foo to /?foo, /bar to ?bar etc.; /bar/12 to /?bar&nr=12 etc. and /foo/somegroup to /foo&group=somegroup
It should work both with and without final slash.
The (admittedly clueless) things I tried either did nothing or resulted in a server error. At some points I also tried just getting the &nr=xx bit or simply the categories. Some of them are:
RewriteCond %{QUERY_STRING} (?:^|&)([^&]+) RewriteRule /?(.*) %1/$1
[QSA]
RewriteRule ^/([^/]+)/([^/]+)$ /?$1&nr=$2
RewriteRule ^/([^/]+) ?nr=$1 [L]
Ok, assuming the file handling the queries is index.php and foo and bar don't change to something else, you could start like this:
RewriteEngine On
RewriteBase /
RewriteRule ^foo$ foo/ [NC,R]
RewriteRule ^foo/$ index.php?foo [NC,L]
RewriteRule ^foo/([A-Za-z0-9_-]+)$ foo/$1/ [NC,R]
RewriteRule ^foo/([A-Za-z0-9_-]+)/$ index.php?foo&group=$1 [NC,L]
Now you should be able to do the bar part on your own!
Btw. if you really want your query string to only be ?foo (without a value, like so: ?foo=somevalue) you'll have to check for foo's existence differently than you would for e.g. the somegroup part.
Looking for some help with some pagination and mod-rewrite
okay so i've got a url that looks like this
www.website.com/page.php?phone=111-393-4949
and I'm rewriting it to look like this...
www.website.com/701-625-5444.php
with this
RewriteRule ^([0-9]+)-([0-9]+)-([0-9]+) page.php?number=$1-$2-$3 [L,QSA]
I'm having some issues with adding my pagination to the mod rewrite now.
I am really not too sure on what all I need to do here but this is what I've come up with
RewriteRule ^([0-9]+)-([0-9]+)-([0-9]+)/([^/]*) page.php?number=$1&page=$2 [L,QSA]
which should output..
www.website.com/494-949-9494/1.php
what is it that I'm doing wrong?
Each () pair is a capture group. Since you already have three () groups for the phone number, the ([^/]*) you added will be captured in $4.
RewriteRule ^([0-9]+)-([0-9]+)-([0-9]+)/([^/]*)$ page.php?number=$1-$2-$3&page=$4 [L,QSA]
If the pagination is optional, add an optional non-capturing group (?:)? around the final segment:
RewriteRule ^([0-9]+)-([0-9]+)-([0-9]+)(?:/([^/]*))?$ page.php?number=$1-$2-$3&page=$4 [L,QSA]
I will note that your RewriteRule does not forcefully include the .php as in your example www.website.com/701-625-5444.php. That would look like
RewriteRule ^([0-9]+)-([0-9]+)-([0-9]+)\.php$ page.php?number=$1-$2-$3 [L,QSA]
# Or...
RewriteRule ^([0-9]+)-([0-9]+)-([0-9]+)(?:/([^/]*))?\.php$ page.php?number=$1-$2-$3&page=$4 [L,QSA]
My original url is : www.site.com/report.cgi?d=2012-05
Requested URL: www.site.com/report-2012-05.cgi
My Htaccess Code:*
RewriteRule ^report([^/]*)\.cgi$ /report.php?d=$1 [L]
I want to restrict the request parameter to just XXXX-XX number format in GET url.
How can I do this ?
I didn't really understand your question, except you want to modify the URL format placing the parameter value in a different position.
The best way to do it is by capturing the query string like this:
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} d=(.*)
The value inside the round brackets is the parameter value (2012-05), which can be back referenced with %1. For example:
RewriteRule .* report-%1.cgi [L]
Will rewrite the URL with /report-2012-05.cgi
Hope this helps.
I think you need to remove .cgi from your rewrite rule
For www.site.com/report-xxxx-xx
RewriteRule ^report-([^/]*)$ /report.cgi?d=$1 [L]<br>
For www.site.com/xxxx-xx
RewriteEngine On
RewriteRule ^([^/]*)$ /report.cgi?d=$1 [L]