why my htaccess rule not works - php

I created a htaccess rule but it doesnot works.
here is my url
http://localhost/mat/site/brandlisting/21/page/2
here is my htaccess rule for it
RewriteRule brandlisting/(.*)(.*)/page/(.*) site/brandlisting?id=$1&page=$2 [L]
RewriteRule brandlisting/(.*)(.*)/page site/brandlisting?id=$1&page=$2 [L]
RewriteRule brandlisting/(.*)/ site/brandlisting?id=$1 [L]
RewriteRule brandlisting/(.*) site/brandlisting?id=$1 [L]
RewriteRule site/brandlisting/(.*)/?$ site/brandlisting?id=$1 [L]
is there anything wrong?
what mistate is in my htaccess rule.

You are using wildcard greedy pattern regex matching everything in single hit, use below rules I am assuming you are using .htaccess in site folder.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^brandlisting/([\d]+)/page/([\d]+)$ brandlisting?id=$1&page=$2 [L]
RewriteRule ^brandlisting/([\d]+)/?$ brandlisting?id=$1 [L]
RewriteRule ^site/brandlisting/([\d]+)/?$ brandlisting?id=$1 [L]

Related

How to change .htaccess rewrite rules to web.config

I have no idea of web.config I have written some rewrite rules in .htacess , but how to convert those in web.config. I found out about IIS but I was not able to understand anything.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [L]
RewriteRule ^payment/([^/]*)$ /vhil/checkout?id=$1 [L]
RewriteRule ^product/([^/]*)$ /vhil/product-desc?p=$1 [L]
#admin rules
RewriteRule ^admin/edit/product/([^/]*)$ /vhil/admin/editProduct?id=$1 [L]
RewriteRule ^admin/edit/banner/([^/]*)$ /vhil/admin/editBanner?id=$1 [L]

php - Weird htaccess mod rewrite acting

I'm trying to set "nice urls" to my website and I have this structure of mod rewrite conditions:
RewriteRule ^/$ /index.php?&%{QUERY_STRING} [L]
RewriteRule ^tabulky-velikosti/?$ tabulky-velikosti.php?&%{QUERY_STRING} [L]
RewriteRule ^o-bambusu/?$ o-bambusu.php?&%{QUERY_STRING} [L]
RewriteRule ^kolekce/?$ kolekce.php?&%{QUERY_STRING} [L]
RewriteRule ^vymena-zbozi/?$ vymena-zbozi.php?&%{QUERY_STRING} [L]
RewriteRule ^doprava-a-platba/?$ doprava-a-platba.php?&%{QUERY_STRING} [L]
RewriteRule ^obchodni-podminky/?$ obchodni-podminky.php?&%{QUERY_STRING} [L]
RewriteRule ^ochrana-osobnich-udaju/?$ ochrana-osobnich-udaju.php?&%{QUERY_STRING} [L]
RewriteRule ^onas/?$ onas.php?&%{QUERY_STRING} [L]
RewriteRule ^contact/?$ kontakt.php?&%{QUERY_STRING} [L]
RewriteRule ^faq/?$ faq.php?&%{QUERY_STRING} [L]
RewriteRule ^kategorie/panske-pradlo/?$ category.php?gender=panske&%{QUERY_STRING} [L]
RewriteRule ^kategorie/damske-pradlo/?$ category.php?gender=damske&%{QUERY_STRING} [L]
RewriteRule ^kategorie/detske-pradlo/?$ category.php?gender=detske&%{QUERY_STRING} [L]
RewriteRule ^(.*)/?$ product.php?url=$1&%{QUERY_STRING} [L]
When I use testing tool (https://htaccess.madewithlove.be), all is good, but when I try to run this on actual website, there is problem with last condition
RewriteRule ^(.*)/?$ product.php?url=$1&%{QUERY_STRING} [L]
It seems that all conditons above are being ignored, for example when I ask for url www.domain.com/contact/, it should show www.domain.com/kontakt.php, but instead of that, it shows page www.domain.com/product.php?url=contact.
When I remove the last line, everything is working as it should.
Can anyone give me some solution to this please? Thanks alot!
.* matches everything and you don't have any RewriteCond above last rule to make that rule conditional.
Besides you have lot of unnecessary use of %{QUERY_STRING} in target URI that you can get with QSA flag.
Fully refactored .htaccess should be like this:
DirectoryIndex index.php
RewriteEngine On
RewriteRule ^tabulky-velikosti/?$ tabulky-velikosti.php [L]
RewriteRule ^o-bambusu/?$ o-bambusu.php [L]
RewriteRule ^kolekce/?$ kolekce.php [L]
RewriteRule ^vymena-zbozi/?$ vymena-zbozi.php [L]
RewriteRule ^doprava-a-platba/?$ doprava-a-platba.php [L]
RewriteRule ^obchodni-podminky/?$ obchodni-podminky.php [L]
RewriteRule ^ochrana-osobnich-udaju/?$ ochrana-osobnich-udaju.php [L]
RewriteRule ^onas/?$ onas.php [L]
RewriteRule ^contact/?$ kontakt.php [L]
RewriteRule ^faq/?$ faq.php [L]
RewriteRule ^kategorie/((?:pan|dam|det)ske)-pradlo/?$ category.php?gender=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)/?$ product.php?url=$1 [L,QSA]
Take note of RewriteCond %{REQUEST_FILENAME} !-f and RewriteCond %{REQUEST_FILENAME} !-d before last rule to prevent execution for valid files and directories.

Rewriting a url with more than one page

I'm trying to rewrite my urls on a page that has a pagination option and I'm unsure of what string to use because the one i'm currently using isn't working.
My document structure is below
blog-listing.php
htaccess
/blog/post.php
/css/
/js/
/images/
What's already in my htaccess file
RewriteEngine on
RewriteRule ^blog/category/([a-zA-Z0-9-/]+)$ /blog/archive.php?cat=$1 [L]
RewriteRule ^blog/category/([a-zA-Z0-9-/]+)/page/([0-9]+)/{0,1}$ /blog/archive.php?cat=$1&page=$2 [L]
RewriteRule ^blog/([a-zA-Z0-9-/]+)$ /blog/post.php?s=$1 [L]
RewriteRule ^blog/([a-zA-Z0-9-/]+)$ /blog/post.php?s=$1&page=$2 [L]
RewriteRule ^(css)($|/) - [L]
RewriteRule ^(perch)($|/) - [L]
RewriteRule ^(images)($|/) - [L]
RewriteRule ^(js)($|/) - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
The line i've tried to use
RewriteRule ^blog-listing/([a-zA-Z0-9-/]+)$ /blog-listing.php?s=$1&page=$2 [L]
The url I'm receiving
http://mysite(dot)com/blog-listing.php?page=2
The url I'd like
http://mysite(dot)com/blog-listing/page-2
Any ideas?
Use a simple file to manage your url
RewriteEngine On
# Redirect all requests not pointing at an actual file to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [L]
https://github.com/ogallardop/blackjack-api/blob/master/src/.htaccess
The framework you're using to pagination will do the rest.

htaccess URL Rewrite Not working with first rule

I have the following rules setup for my blog and they seem to work just fine.
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/(index\.php|(modules|css|files|fonts|ico|img|js)/)
RewriteRule ^([^/]*)/page/([^/]*)$ /framework/?p=$1&page=$2 [L]
RewriteRule ^([^/]*)/search/([^/]*)$ /framework/?p=$1&search=$2 [L]
RewriteRule ^([^/]*)/search/([^/]*)/page/([^/]*)$ /framework/?p=$1&search=$2&page=$3 [L]
So I was feeling confident and wanted to add one more rule for the just the basic page.
so I added this rule
RewriteRule ^([^/]*)$ /framework/?p=$1 [L]
so that my htaccess file now looks like this
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/(index\.php|(modules|css|files|fonts|ico|img|js)/)
RewriteRule ^([^/]*)$ /framework/?p=$1 [L]
RewriteRule ^([^/]*)/page/([^/]*)$ /framework/?p=$1&page=$2 [L]
RewriteRule ^([^/]*)/search/([^/]*)$ /framework/?p=$1&search=$2 [L]
RewriteRule ^([^/]*)/search/([^/]*)/page/([^/]*)$ /framework/?p=$1&search=$2&page=$3 [L]
but for if I go to http://www.example.com/framework/blog I get a 500 Internal Server Error if I take out the line and go to http://www.example.com/framework/blog/page/2 it loads my second page without any issues.
I am not sure what I am doing wrong? Any advice would be appreciated.
You can use:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /framework/
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule ^([^/]+)/page/([^/]*)$ ?p=$1&page=$2 [L,QSA]
RewriteRule ^([^/]+)/search/([^/]*)/page/([^/]*)$ ?p=$1&search=$2&page=$3 [L,QSA]
RewriteRule ^([^/]+)/search/([^/]*)$ ?p=$1&search=$2 [L,QSA]
RewriteRule ^([^/]+)/?$ ?p=$1 [L,QSA]

Combine htaccess rewrites

I am using php CodeIgniter and have the following rewrite rule in my .htaccess file:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
Since I want the users to use the site through SSL, I would like to add the following rewrite rule:
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.link.com/$1 [R,L]
However, when I do that, every page in a subdirectory will have /index.php/actual_page - the index.php before that is not desired. I thought that might come from having RewriteRule ^(.*)$ index.php/$1 [L] in there, but without the RewriteRule ^(.*)$ https://www.link.com/$1 [R,L] it works fine.
So, how can I rewrite the url to https without breaking the first rewrite?
Make sure the https redirect happens before the rewrite to index.php. You want it to look something like this:
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.link.com/$1 [R,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

Categories