I want use rewrite rule for categories
But my code work for first category only
My code :
RewriteRule ^category/([^/]+)/$ category.php?url=$1 [NC,L]
My Test :
http://nnn.com/category/a/ -> work
http://nnn.com/category/a/b/ -> not work
http://nnn.com/category/a/b/c/ -> not work
You may change your rule to this:
RewriteRule ^category/(.+)$ category.php?url=$1 [NC,QSA,L]
.+ pattern instead of [^/]+ will allow 1+ of any characters including / in parameter url.
Related
In the .htaccess file of my PHP application I use one simple rewrite to translate my URLs from:
/shop/hats/detroit/
to:
index.php?url=/shop/hats/detroit/
using the following rewrite rule:
# Rule 1
RewriteRule ^(.*)$ /repos/nvp/httpdocs/index.php?url=/$1 [L,QSA]
I would like the following rewrite to work as well:
# Rule 2
RewriteRule ^pic/(.*)$ /repos/nvp/httpdocs/get_image.php?pic=$1 [L,QSA]
But the two rules are kind of overwriting or conflicting with each other. They work each on their own but not together. How can I use all urls to use Rule 1 and only if url starts with pic/ to use Rule 2 instead of Rule 1?
This is because the pattern (.*) matches all uris .
In order to avoid the rules overriding ,you need to reorder your rules and put the specific rules first in order.
# specific rules
RewriteRule ^pic/(.*)$ /repos/nvp/httpdocs/get_image.php?pic=$1 [L,QSA]
#catch-all rules
RewriteRule ^(.*)$ /repos/nvp/httpdocs/index.php?url=/$1 [L,QSA]
We currently have a .htaccess RewriteRule that's incorrectly (or correctly as the rule is incorrect) redirecting a URL.
The Rule
RewriteRule ^holiday-ecards/?.*$ /appindex.php [L]
The desired redirects for this are:
http://domain.com/holiday-ecards/
http://domain.com/holiday-ecards/1/
http://domain.com/holiday-ecards/1/2
http://domain.com/holiday-ecards/1/2/3
However, it seems to also be redirecting the following, which is undesired:
http://domain.com/holiday-ecards-business/
EDIT
/appindex.php
This is taking care of the app routing and works as intended.
A number of ways you could do it, one would be setting a rewrite condition to not touch URI's that have holiday-ecards plus hyphen, like so:
RewriteCond %{REQUEST_URI} !^/holiday-ecards-.*$
RewriteRule ^holiday-ecards/?.*$ /appindex.php [L]
Not sure how many variations you have of URI's with holiday-ecards in them.
RewriteRule ^holiday-ecards/?.*$ /appindex.php [L]
(Note that this is an internal rewrite, not a redirect.)
The above RewriteRule pattern makes the slash after holiday-ecards optional (so it will also match holiday-ecards-business). However, in the example URLs that should be rewritten, the slash is mandatory. So, it would appear that you just need to make it mandatory (?), for example:
RewriteRule ^holiday-ecards/ /appindex.php [L]
The trailing pattern .*$ is superfluous.
I have two pages say
http://assistque.com/services.php?prod_id=35&prod_url=Operating%20System%20–%20Windows
and
http://assistque.com/sub_services.php?sp_id=13&sp_url=Technical%20Support%20For%20KASPERSKY
i wrote two rules for two different pages but the for the second page it uses first rule .
Please help
RewriteRule ^([^/]*)/([^/]*)\.html$ /services.php?prod_id=$1&prod_url=$2
RewriteRule ^([^/]*)/([^/]*)\.html$ /sub_services.php?sp_id=$1&sp_url=$2
This is because the patterns in the both rules are the same, try changing them into something unique:
RewriteRule ^services/([^/]*)/([^/]*)\.html$ /services.php?prod_id=$1&prod_url=$2 [L]
RewriteRule ^subservice/([^/]*)/([^/]*)\.html$ /sub_services.php?sp_id=$1&sp_url=$2 [L]
Then in your links you add something like:
operating systems windows
Note, that you should not use spaces inside an url, so in this case I assume those will be replaced with an - character.
It may also be better to narrow down allowed characters and numbers in your rewrite rule:
# will match services/<number>/<string>.html
# the NC flag states that the rule is case insensitive
RewriteRule services/([0-9]+)/([a-z-]+)\.html$ /services.php?prod_id=$1&prod_url=$2 [NC,L]
Below is my code for .htaccess
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)/([0-9]+)$ /products/product-full-view.php?src=$1&id=$2
RewriteRule ^([a-zA-Z0-9_-]+)/([0-9]+)/?$ /products/product-full-view.php?src=$1&id=$2
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)/([0-9]+)$ /buy/buy-full-view.php?src=$1&id=$2
RewriteRule ^([a-zA-Z0-9_-]+)/([0-9]+)/?$ /buy/buy-full-view.php?src=$1&id=$2
First rule is working fine but its not taking the second rule...Unable to understand what is happening here...
Original URL's are like this
www.example.com/products/product-full-view.php?src=somevalue&id=somevalue
and for second one
www.example.com/buy/buy-full-view.php?src=somevalue&id=somevalue
Please help me to run second rule also.
Thanks in advance
You're trying to match the same pattern, give or take an optional trailing slash, four times. By the time you reach the last two rules, your URL is already rewritten to something else.
You probably want something that looks more like:
RewriteEngine On
RewriteRule ^products/([0-9]+)/?$ /products/product-full-view.php?id=$1 [L]
RewriteRule ^buy/([0-9]+)/?$ /buy/buy-full-view.php?id=$1 [L]
Or:
RewriteEngine On
RewriteRule ^products/([a-zA-Z0-9_-]+)/([0-9]+)/?$ /products/product-full-view.php?src=$1&id=$2 [L]
RewriteRule ^buy/([a-zA-Z0-9_-]+)/([0-9]+)/?$ /buy/buy-full-view.php?src=$1&id=$2 [L]
Or something to that order anyway.
Note the stub and the [L] each time: the products/ and buy/ avoid that the same URL refers to two different locations, and the [L] (for "Last") tells the rewrite engine to stop processing rules when it gets matched.
If a URL matches the second rule, it also matches the first rule. After applying the first rule, the resulting URL no longer matches the second rule. That's why the second rule is never applied.
I'm using mod_rewrite for rewriting my links as follows. I defined a redirect from /test/1234_5678_... to /test.php?id=1234 as follows:
RewriteRule test/(.*)_(.*)$ test.php?id=$1
It works perfectely. Now I wanted to add the following redirect: /test/1234_5678_.../print to /test.php?id=1234&print. Therefore I added the following line before the one above. The redirect is not working and it seems as if only the second rule applies. Am I doing anything wrong with the pattern matching? Is it a problem that there can be more than one underscore and I only used one in the pattern?
RewriteRule test/(.*)_(.*)/print$ test.php?id=$1&print
RewriteRule test/(.*)_(.*)$ test.php?id=$1
Both rules work fine for me, but you probably want to change the first grouping to ([0-9]+) or ([^_]+), and the second group to [^/]+, and add some L flags:
RewriteRule test/([^_]+)_([^/]+)/print$ test.php?id=$1&print [L]
RewriteRule test/([^_]+)_([^/]+)$ test.php?id=$1 [L]