i have a example for question :
I use 3 line for rewrite
RewriteRule ^category.html$ index.php?page=category&type=all&page_value=1
RewriteRule ^category/([a-zA-Z0-9\-\_]+).html$ index.php?page=category&type=$1&page_value=1
RewriteRule ^category/([a-zA-Z0-9\-\_]+)/([0-9]+).html$ index.php?page=category&type=$1&page_value=$2
Can i rewriteRule in 1 line?
Kind of - you could do this:
RewriteRule ^category(?:(?:/([a-zA-Z0-9\-\_]+))?(?:/([0-9]+))?)?.html index.php?page=category&type=$1&page_value=$2
Demo: http://htaccess.mwl.be?share=ea336c72-76e4-5324-863d-ce32c04573c3
BUT - see how for the base category this does not pass values to your query string so
http://example.com/category.html
rewrites to
http://example.com/index.php?page=category&type=&page_value=
You would therefore need to ensure your parsing script can handle this with default params for type and page_value.
Related
I'm trying to figure out how to set up a mod_rewrite rule so that a request for a page, with a URL parameter appended, determines the root from which that file is served.
Here's the setup. In my "foo" directory I have "bar", "bar19", "bar27", etc.
Ideally I'd like to match on the first two characters of the "v" parameter. So like this:
I would like a request for ..................... to be served from:
www.example.com/foo/bar/something.html .......... foo/bar/something.html
www.example.com/foo/bar/something.html?v=19xxx ... foo/bar19/something.html
www.example.com/foo/bar/something.html?v=27xxx ... foo/bar27/something.html
Of course I would expect that if a value for "v" parameter that doesn't have a corresponding directory to 404.
I've done some Mod_Rewrite magic before, but I'm kind of stuck here. Any ideas?
Add a .htaccess file in directory /foo with the following content. Of course you can also insert it into your httpd.conf:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /foo
# match query parameter v with two-digit value; capture
# the value as %2 and the query string before and after v
# as %1 and %3, respectively
RewriteCond %{QUERY_STRING} ^(.*)v=(\d\d)[^&]*(&.*)?$
# match path into "bar" and rest; insert two-digit value from
# RewriteCond inbetween; append rest of query string without v value;
# use case-insensitivity
RewriteRule ^(bar)(.*)$ $1%2$2?%1%3 [NC]
</IfModule>
I think the key is to use captured values from the RewriteCond (accessible as %1, %2, etc.) and at the same time captured values from the RewriteRule itself (as usual $1, $2, etc.).
I'm trying to do sorting passing get value in htaccess to php:
I have sth like:
RewriteRule ^pubs$ pubs.php
RewriteRule ^pubs/beer$ pubs.php?sort=beer
Above code is working fine. Now Im trying to do the same thing for subpages (cities):
RewriteRule ^pubs/(.*)$ district.php?name=$1
RewriteRule ^pubs/(.*)/beer$ district.php?name=$1&sort=beer
The links look like:
www.domain.com/pubs/beer - working ok
www.domain.com/pubs/new-york/beer - not working
What I'm doing wrong? If i delete line RewriteRule ^pubs/(.*)$ district.php?name=$1 then the link www.domain.com/pubs/new-york/beer is working. I spend hours to find solution with no luck. I will be grateful for help.
If i delete line RewriteRule ^pubs/(.*)$ district.php?name=$1 then the
link www.domain.com/pubs/new-york/beer is working.
because www.domain.com/pubs/new-york/beer is matching by
RewriteRule ^pubs/(.*)$ district.php?name=$1
resulting in district.php?name=newyork/beer instead of matching
RewriteRule ^pubs/(.*)/beer$ district.php?name=$1&sort=beer
one way you could fix this would be:
RewriteRule ^pubs/([^/]*)$ district.php?name=$1
so if there is an additional slash anywhere after your city the rule will be ignored and proceed to the 2nd rule... just be sure to put it after
RewriteRule ^pubs/beer$ pubs.php?sort=beer
or it will match beer as if it were a city name like district.php?name=beer
I have a php page which creates URL like:
vendors/London City/cat-DJ & Entertainment/keywords
which my .htaccess redirects as shown below
RewriteRule vendors/(.+)/cat-(.+)/(.+)$ vendors.php?location=$1&category=$2&freetext=$3 [L]
RewriteRule vendors/(.+)/cat-(.+)/(.+)/$ vendors.php?location=$1&category=$2&freetext=$3 [L]
problem 1 is : in the vendors.php file, I am getting only "DJ ; Entertainment" as category. The ampersand is missing.
Problem 2 is : My complete .htaccess file is shown below... 6 rules are defined.
RewriteRule vendors/(.+)/(.+)/$ vendors.php?location=$1&freetext=$2 [L]
RewriteRule vendors/(.+)/(.+)$ vendors.php?location=$1&freetext=$2 [L]
RewriteRule vendors/(.+)/cat-(.+)/$ vendors.php?location=$1&category=$2 [L]
RewriteRule vendors/(.+)/cat-(.+)$ vendors.php?location=$1&category=$2 [L]
RewriteRule vendors/(.+)/cat-(.+)/(.+)$ vendors.php?location=$1&category=$2&freetext=$3[L]
RewriteRule vendors/(.+)/cat-(.+)/(.+)/$ vendors.php?location=$1&category=$2&freetext=$3[L]
Why the URL vendors/London City/cat-DJ & Entertainment/keywords is matching with rule 3 or 4 and redirecting to vendors.php?location=$1&category=$2 ?
Does .htaccess Process the rules from top to beginning one by one?
I had solved the problem by putting the rules 5 and 6 at the top of other rules. Did I make the correct fix?
1. I don't really like the idea of having spaces and other special characters in the URLs. I don't know if it's possible with your site, but instead of this kind of URL
vendors/London City/cat-DJ & Entertainment/keywords
you should have this one:
vendors/london-city/cat-dj-and-entertainment/keywords
For that, of course, you will have to perform some additional transformations / lookups in your database to convert london-city back to London City and dj-and-entertainment back to DJ & Entertainment. This can be done by storing these "from-to" pairs in database.
2. In any case -- order of rules matters. Therefore you should start with more specific rules and end up with more generic rules.
Also -- the (.+) pattern is a way too broad as it can match hello as well as hello/pink/kitten. To ensure that you always grab only one section (part of URL between /) use ([^/]+) pattern instead -- this will address one of the aspects of your "prob #2".
Therefore, try these optimized rules (each rule will match the URL with and without trailing slash):
RewriteRule ^vendors/([^/]+)/cat-([^/]+)/([^/]+)/?$ vendors.php?location=$1&category=$2&freetext=$3 [L]
RewriteRule ^vendors/([^/]+)/cat-([^/]+)/?$ vendors.php?location=$1&category=$2 [L]
RewriteRule ^vendors/([^/]+)/([^/]+)/?$ vendors.php?location=$1&freetext=$2 [L]
Also I'm not getting the value of 'category' with the Ampersand as
given in the url. I am getting only semi-colon. What can be the
reason?
I do not have Apache box currently running next to me, so cannot check it right now, but try adding B or NE flag next to the L flag (e.g. [L,B]) -- one of them should help:
http://httpd.apache.org/docs/current/rewrite/flags.html#flag_b
http://httpd.apache.org/docs/current/rewrite/flags.html#flag_ne
From the docs:
The order in which these rules are defined is important - this is the order in which they will be applied at run-time.
I have a folder named /test in my application.
Right now i am trying to write an .htaccess file that would show all requests to /test* as /test.
For example:
www.example.com/test/ is the actual directory with index.php file in it.
All the requests like the following should go to the same /test directory
www.example.com/test-hello/
www.example.com/test-world/
www.example.com/test-htacess/
www.example.com/test123/
Basically any requests to /test* should go to /test.
This is what I've tried so far:
RewriteRule ^/test* /test
You need to use RewriteCond to first match "test in url"
Try below:
RewriteCond %{THE_REQUEST} ^GET\ /test/
RewriteRule ^test/(.*) /test/$1 [L,R=301]
Your regular expression is wrong. You mean ^/test.*$. Your rule would match to /testtttt.
The asterisk means that the char in front of it can be zero or more times included. The dot is a special char which means here could be anything. the .* matches every string including an empty string. See also Wikipedia.
You currently are not putting the -hello, -world etc behind your folder. What is hello? Is that the file? Or the param?
The second part of the rewriteRule should be a file. Something like
RewriteRule ^/test(.*)$ /test/$1.php
Above function will have:
/testABC to /test/ABC.php
But I don't understand what you want to accomplish?
I would like to have
eg.
www.domain.com/api/json/implode/abc/and/a/b/c...
equal to
www.domain.com/api/jason/?method=implode&key=abc¶m1=and¶m2=a¶m3=b¶m4=c...
You could do something like this:
RewriteRule ^/api/json/([/]*)/([/]*)$ /api/jason/method=$1&key=$2
RewriteRule ^/api/json/([/]*)/([/]*)/([/]*)$ /api/jason/method=$1&key=$2¶m1=$3
RewriteRule ^/api/json/([/]*)/([/]*)/([/]*)/([/]*)$ /api/jason/method=$1&key=$2¶m1=$3¶m2=$4
RewriteRule ^/api/json/([/]*)/([/]*)/([/]*)/([/]*)/([/]*)$ /api/jason/method=$1&key=$2¶m1=$3¶m2=$4¶m3=$5
RewriteRule ^/api/json/([/]*)/([/]*)/([/]*)/([/]*)/([/]*)/([/]*)$ /api/jason/method=$1&key=$2¶m1=$3¶m2=$4¶m3=$5¶m4=$6
# keep expanding this pattern out for however many paramas you need to be
# able to handle...
But cleaner would be something like this:
RewriteRule ^/api/json/([/]*)/([/]*)$ /api/jason/method=$1&key=$2
RewriteRule ^/api/json/([/]*)/([/]*)/(.*)$ /api/jason/method=$1&key=$2¶ms=$3
ie: stuff all trailing optional params into a single parameter.
I don't think it's possible to dynamically figure out the number of params, but you can do something close:
RewriteRule ^/api/json/([^/]+)/([^/]+)/(.*) /api/jason/?method=$1&key=$2¶ms=$3
This will extract method and key individually but put all the additional parameters into params, which you can split later within your script.