php - Weird htaccess mod rewrite acting - php

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.

Related

I want to use rewriterule to redirect this url to this url

http://domain.co.kr/areasearch?sfl=wr_7&stx=apple
I want to use rewriterule to redirect this url to this url
http://domain.co.kr/areasearch/wr_7/apple
I added the below rewriterule regex at the bottom
RewriteRule ^/([^/]+)/([^/]+)/?$ areasearch?sfl=$1&stx=$2 [QSA,L]
When I access the url below, I get a Not Found The requested URL /wr_7/apple was not found on this server. What's the problem?
http://domain.co.kr/areasearch/wr_7/apple
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^shop/list-([0-9a-z]+)$ shop/list.php?ca_id=$1&rewrite=1 [QSA,L]
RewriteRule ^shop/type-([0-9a-z]+)$ shop/listtype.php?type=$1&rewrite=1 [QSA,L]
RewriteRule ^shop/([0-9a-zA-Z_\-]+)$ shop/item.php?it_id=$1&rewrite=1 [QSA,L]
RewriteRule ^shop/([^/]+)/$ shop/item.php?it_seo_title=$1&rewrite=1 [QSA,L]
RewriteRule ^content/([0-9a-zA-Z_]+)$ bbs/content.php?co_id=$1&rewrite=1 [QSA,L]
RewriteRule ^content/([^/]+)/$ bbs/content.php?co_seo_title=$1&rewrite=1 [QSA,L]
RewriteRule ^rss/([0-9a-zA-Z_]+)$ bbs/rss.php?bo_table=$1 [QSA,L]
RewriteRule ^([0-9a-zA-Z_]+)$ bbs/board.php?bo_table=$1&rewrite=1 [QSA,L]
RewriteRule ^([0-9a-zA-Z_]+)/([^/]+)/$ bbs/board.php?bo_table=$1&wr_seo_title=$2&rewrite=1 [QSA,L]
RewriteRule ^([0-9a-zA-Z_]+)/write$ bbs/write.php?bo_table=$1&rewrite=1 [QSA,L]
RewriteRule ^([0-9a-zA-Z_]+)/([0-9]+)$ bbs/board.php?bo_table=$1&wr_id=$2&rewrite=1 [QSA,L]
RewriteRule ^/([^/]+)/([^/]+)$ areasearch?sfl=$1&stx=$2 [QSA,L]
</IfModule>
Your regex (^/([^/]+)/([^/]+)$) doesn't match areasearch/wr_7/apple
You probably missed that your URL starts with areasearch/ so this should work
^areasearch/([^/]+)/([^/]+)$

php - error document combined with mod_rewrite

I'm trying to set ErrorDocument 404 page by using ErrorDocument 404 /404.php, but it does not work, because I have mod_rewrite enabled... Is there some chance to check, if the page exist before it falls to mod_rewrite? I'm posting my htaccess down bellow... It is caused by last rule, which redirects everything to product.php - RewriteRule ^(.+?)/?$ product.php?url=$1 [L,QSA]
DirectoryIndex index.php
RewriteEngine On
ErrorDocument 404 /404.php
RewriteRule ^(admin|subdom)($|/) - [L]
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteRule ^coming-soon/?$ coming-soon.php [L]
RewriteRule ^tabulky-velikosti/?$ tabulky-velikosti.php [L]
RewriteRule ^o-bambusu/?$ o-bambusu.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 ^o-nas/?$ onas.php [L]
RewriteRule ^kontakt/?$ kontakt.php [L]
RewriteRule ^faq/?$ faq.php [L]
RewriteRule ^kosik/?$ cart.php [L]
RewriteRule ^blog/?$ blog.php [L]
RewriteRule ^search/?$ search.php [L]
RewriteRule ^blog/tag/(.*)/? blog.php?tag=$1 [L,QSA]
RewriteRule ^blog/(.*)/? blogDetail.php?url=$1 [L,QSA]
RewriteRule ^search/(.*)/? search.php?s=$1 [L,QSA]
RewriteRule ^zaciname/?$ category.php [L]
RewriteRule ^nakupovat/?$ category.php [L]
RewriteRule ^kategorie/?$ category.php [L]
RewriteRule ^prozkoumat/?$ category.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]
Is there anything I can do with that?
There is another way in which you can do this. You set the 404 via a RewriteRule and then set the ErrorDocument via URL instead of the file:
RewriteRule ^404/?$ 404.php
ErrorDocument 404 https://www.example.com/404.php
Place it below your last rule and remove the [L] flag from your last rule.
This method shouldn't cause an issue with mod_rewrite. Make sure you clear your cache before testing this.
EDIT
You could just add a condition to the last rule to exclude the 404.php page?
RewriteCond %{REQUEST_URI} !^/404.php
That would stop the Rewrite from happening for that page, so your code would be:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/404.php
RewriteRule ^(.+?)/?$ product.php?url=$1 [L,QSA]

why my htaccess rule not works

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]

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]

Removing .php extension with mod_rewrite works but tags pages does not

I have such .htaccess file:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteRule (.*)/sites/([^a-zA-Z][0-9_/]*$) $1.php?sites=$2 [L]
RewriteRule (.*)/sites/([0-9_/]*)/(.*) $3 [L]
RewriteRule (.*)/sites/(.*) $2 [L]
RewriteRule tags_xml/(.*).xml tags/display_xml.php?tag=$1 [L]
RewriteRule news_xml/(.*).xml rss.php?site=$1 [L]
RewriteRule tags/([ÄĂ“ĹŚĄŻŹĆĹęółśążźćńa-zA-Z0-9%\ ]*)/([^a-zA-Z][0-9_/]*$) tags/display.php?tag=$1&page=$2 [L]
RewriteRule tags/([ÄĂ“ĹŚĄŻŹĆĹęółśążźćńa-zA-Z0-9%\ ]+) tags/display.php?tag=$1 [L]
RewriteRule news/([0-9]*)/([0-9]*)/([^a-zA-Z][0-9]*)/(.*).html$ kom.php?id_b=$1&id_n=$2&kom_page=$3 [L]
RewriteRule news/([0-9]*)/([0-9]*)/(.*).html$ kom.php?id_b=$1&id_n=$2 [L]
RewriteRule news/([0-9]*)/([0-9]*)/([^a-zA-Z][0-9]*)/(.*) $4 [L]
RewriteRule news/([0-9]*)/([0-9]*)/(.*) $3 [L]
RewriteCond %{THE_REQUEST} ^.*index.php.*
Now when I enter
www.domain.com/file
instead of
www.domain.com/file.php
it works ok, but when I enter
www.domain.com/tags/Bank
I got no such tag found error. Also other scripts in this folder just display "no such tag found".
The previous working code was:
RewriteEngine On
RewriteBase /
RewriteRule (.*)/sites/([^a-zA-Z][0-9_/]*$) $1.php?sites=$2 [L]
RewriteRule (.*)/sites/([0-9_/]*)/(.*) $3 [L]
RewriteRule (.*)/sites/(.*) $2 [L]
RewriteRule tags_xml/(.*).xml tags/display_xml.php?tag=$1 [L]
RewriteRule news_xml/(.*).xml rss.php?site=$1 [L]
RewriteRule tags/([ĘÓŁŚĄŻŹĆŃęółśążźćńa-zA-Z0-9%\ ]*)/([^a-zA-Z][0-9_/]*$) tags/display.php?tag=$1&page=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule tags/([ĘÓŁŚĄŻŹĆŃęółśążźćńa-zA-Z0-9%\ ]+) tags/display.php?tag=$1 [L]
RewriteRule news/([0-9]*)/([0-9]*)/([^a-zA-Z][0-9]*)/(.*).html$ kom.php?id_b=$1&id_n=$2&kom_page=$3 [L]
RewriteRule news/([0-9]*)/([0-9]*)/(.*).html$ kom.php?id_b=$1&id_n=$2 [L]
RewriteRule news/([0-9]*)/([0-9]*)/([^a-zA-Z][0-9]*)/(.*) $4 [L]
RewriteRule news/([0-9]*)/([0-9]*)/(.*) $3 [L]
RewriteCond %{THE_REQUEST} ^.*index.php.*
Update:
OK, after reading through your htaccess again, I notice that you use the following:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
On some systems this will point relative to your filesystem, so maybe adding DOCUMENT_ROOT will work for you. Like so:
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
The solution was to just add at the end:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.php [NC,L]

Categories