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]
Related
I am trying to redirect /search.php?s=xxx to /search/xxx and actually show /search/xxx in client's address bar.
I am using the following instruction in the htaccess root and it works but doesn't change the URL:
RewriteRule ^search/(.*)$ /search.php?s=$1
I have tried adding [R=301] at the end of the statement, so:
RewriteRule ^search/(.*)$ /search.php?s=$1 [R=301]
But this does the opposite, meaning it changes /search/xxx to /search.php?s=xxx
Here is the entire .htaccess file:
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^monumentum$ /new_monumentum.php
RewriteRule ^monumentum/(.*)$ /new_monumentum.php?mid=$1
RewriteRule ^nuntium$ /new_articulo.php
RewriteRule ^nuntium/(.*)$ /new_articulo.php?aid=$1
RewriteRule ^liber$ /new_liber.php
RewriteRule ^liber/(.*)$ /new_liber.php?lid=$1
RewriteRule ^introductio$ /new_pagina.php
RewriteRule ^introductio/(.*)$ /new_pagina.php?pid=$1
RewriteRule ^persona$ /new_profile.php
RewriteRule ^persona/(.*)$ /new_profile.php?cid=$1
#RewriteRule ^search$ /new_search.php
#RewriteRule ^search/(.*)$ /new_search.php?sea=$1
RewriteRule ^exitio$ /new_exit.php
RewriteRule ^intro$ /new_enter.php
RewriteRule ^novus$ /new_account.php
# Error 404: Paginas no encontradas
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d
ErrorDocument 403 /403.html
ErrorDocument 404 /404.html
To redirect /search.php?s=xxx to /search/xxx, you may use this rule:
Options -MultiViews
RewriteEngine On
RewriteCond %{THE_REQUEST} /search\.php\?s=([^\s&]+) [NC]
RewriteRule ^ /search/%1? [R=301,L,NE]
RewriteRule ^search/(.+)$ search.php?s=$1 [L,NC,QSA]
# place your remaining rules below here
RewriteRule ^monumentum$ new_monumentum.php [L]
RewriteRule ^monumentum/(.*)$ new_monumentum.php?mid=$1 [L,QSA]
RewriteRule ^nuntium$ new_articulo.php [L]
RewriteRule ^nuntium/(.*)$ new_articulo.php?aid=$1 [L,QSA]
RewriteRule ^liber$ new_liber.php [L]
RewriteRule ^liber/(.*)$ new_liber.php?lid=$1 [L,QSA]
RewriteRule ^introductio$ new_pagina.php [L]
RewriteRule ^introductio/(.*)$ new_pagina.php?pid=$1 [L,QSA]
RewriteRule ^persona$ new_profile.php [L]
RewriteRule ^persona/(.*)$ new_profile.php?cid=$1 [L,QSA]
RewriteRule ^exitio$ new_exit.php [L]
RewriteRule ^intro$ new_enter.php [L]
RewriteRule ^novus$ new_account.php [L]
# Error 404: Paginas no encontradas
ErrorDocument 403 /403.html
ErrorDocument 404 /404.html
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.
My target is to remove .php extensions from url and create Semantic URL multilingual site. Here is my .htaccess rules:
# Turn mod_rewrite on
RewriteEngine On
#Remove .php extensions from files
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f
RewriteRule ^(.+?)/?$ /$1.php [L]
#Create Semantic url
RewriteRule ^event/([A-Za-z0-9-\+]+)/([A-Za-z0-9-\+]+)/?$ calendar?region=$1&news_id=$2 [L,NC]
#Handle language switch.
RewriteRule ^(ru)/(.*)$ $2?sitelang=2 [L,QSA]
RewriteRule ^(ru)$ $1?sitelang=2 [L,QSA]
RewriteRule ^(en)/(.*)$ $2?sitelang=1 [L,QSA]
RewriteRule ^(en)$ $1?sitelang=1 [L,QSA]
RewriteRule ^(.*)$ $1?sitelang=1 [L,QSA]
#404 Document /404.php
ErrorDocument 404 /404
So I have some cases when this rules work but also when does not work. So here are the working ones:
domain.com/ru/event/region/some-event-name
domain.com/event/region/some-event-name
And the ones wich does not work(when I add something,bold), causes 500 error:
domain.com/events/region/some-event-name
domain.com/events/region/some-event-name/
domain.com/event/region/some-event-name/something
You need to stop rule execution for existing files and directories. Have your rules as this:
#404 Document /404.php
ErrorDocument 404 /404
# Turn mod_rewrite on
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
#Remove .php extensions from files
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f
RewriteRule ^(.+?)/?$ /$1.php [L]
#Create Semantic url
RewriteRule ^event/([\w-]+)/([\w-]+)/?$ calendar?region=$1&news_id=$2 [L,NC,QSA]
#Handle language switch.
RewriteRule ^(ru)/(.*)$ $2?sitelang=2 [L,QSA]
RewriteRule ^(ru)$ $1?sitelang=2 [L,QSA]
RewriteRule ^(en)/(.*)$ $2?sitelang=1 [L,QSA]
RewriteRule ^(en)$ $1?sitelang=1 [L,QSA]
RewriteRule ^(.*)$ $1?sitelang=1 [L,QSA]
Thanks #anubhava for pointing me to right direction. I was able to solve my problem with your help. Here is the working solution.
#404 Document /404.php
ErrorDocument 404 /404.php
# Turn mod_rewrite on
RewriteEngine On
#Remove .php extensions from files
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f
RewriteRule ^(.+?)/?$ /$1.php [L]
RewriteCond %{REQUEST_FILENAME} !-f [OR]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ - [L]
#Create Semantic url
RewriteRule ^event/([\w-]+)/([\w-]+)/?$ calendar?region=$1&news_id=$2 [L,NC,QSA]
#Handle language switch.
RewriteRule ^(ru)/(.*)$ $2?sitelang=2 [L,QSA]
RewriteRule ^(en)/(.*)$ $2?sitelang=1 [L,QSA]
RewriteRule ^(.*)$ $1?sitelang=1 [L,QSA]
I am setting up my htaccess and everything is working the way I want it to:
Options +FollowSymLinks
RewriteEngine on
# redirect www to non-www
RewriteCond %{HTTP_HOST} ^www\.(([a-z0-9_]+\.)?mydomain\.com)$ [NC]
RewriteRule .? http://%1%{REQUEST_URI}/ [R=301,L]
#rewrite pages
RewriteRule ^photo/([^/]*)/([^/]*)$ /photo.php?slug=$1 [L]
RewriteRule ^video/([^/]*)/([^/]*)$ /video.php?slug=$1 [L]
RewriteRule ^quote/([^/]*)/([^/]*)$ /quote.php?slug=$1 [L]
RewriteRule ^post/([^/]*)/([^/]*)$ /post.php?slug=$1 [L]
RewriteRule ^author/([^/]*)/([^/]*)$ /author.php?display_name=$1 [L]
RewriteRule ^tag/([^/]*)/([^/]*)$ /tag.php?tag_name=$1 [L]
RewriteRule ^category/([^/]*)/([^/]*)$ /category.php?slug=$1 [L]
RewriteRule ^blog/$ /blog.php [L]
RewriteRule ^videos/$ /videos.php [L]
RewriteRule ^photos/$ /photos.php [L]
RewriteRule ^coming-soon/$ /coming-soon.php [L]
RewriteRule ^404/$ /404.php [L]
#add trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^.*[^/]$ /$0/ [L,R=301]
#error redirect
ErrorDocument 404 /404
How would I go about setting it up so that index.php?page=2 redirects to mydomain.com/2/
You can use:
#error redirect
ErrorDocument 404 /404
Options +FollowSymLinks
RewriteEngine on
# redirect www to non-www
RewriteCond %{HTTP_HOST} ^www\.(([a-z0-9_]+\.)?mydomain\.com)$ [NC]
RewriteRule .? http://%1%{REQUEST_URI}/ [R=301,L]
#add trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^.*[^/]$ /$0/ [L,R=301]
#rewrite pages
RewriteRule ^photo/([^/]*)/([^/]*)$ /photo.php?slug=$1 [L]
RewriteRule ^video/([^/]*)/([^/]*)$ /video.php?slug=$1 [L]
RewriteRule ^quote/([^/]*)/([^/]*)$ /quote.php?slug=$1 [L]
RewriteRule ^post/([^/]*)/([^/]*)$ /post.php?slug=$1 [L]
RewriteRule ^author/([^/]*)/([^/]*)$ /author.php?display_name=$1 [L]
RewriteRule ^tag/([^/]*)/([^/]*)$ /tag.php?tag_name=$1 [L]
RewriteRule ^category/([^/]*)/([^/]*)$ /category.php?slug=$1 [L]
RewriteRule ^blog/$ /blog.php [L]
RewriteRule ^videos/$ /videos.php [L]
RewriteRule ^photos/$ /photos.php [L]
RewriteRule ^coming-soon/$ /coming-soon.php [L]
RewriteRule ^404/$ /404.php [L]
RewriteRule ^(\d+)/?$ /index.php?page=$1 [L,QSA]
Always keep redirect rules before internal rules and keep catch-all type rules at the bottom.
I'm working on my website and I created a few new web pages that explains more details of my website. Now for seo purposes I know google prefers for the link to contain the key words. So the pages I'm making could be call new shoes. Now I have gotten the pages to work as website.com/newshoes but since google may view this as one word, I'm working on making it display website.com/new-shoes instead using htaccess but I can't get it to work. This is what I came up with but it didn't work.
RewriteRule ^([^/]*)-([^/]*)$ /index.php?$1$2 [QSA,L]
So yeah can anyone help me out here. Thanks!
[edit]
RewriteEngine On
RewriteCond %{HTTP_HOST} ^reflap\.com [NC]
RewriteRule (.*) http://www.reflap.com/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)$ /index.php?$1 [QSA,L]
RewriteRule ^([^/]*)/$ /index.php?$1 [QSA,L]
RewriteRule ^([^/]*)/([^/]*)$ /index.php?$1&$2 [QSA,L]
RewriteRule ^([^/]*)/([^/]*)/$ /index.php?$1&$2 [QSA,L]
RewriteRule ^/facebook-chat/ http://chat.facebook.com/ [P]
ErrorDocument 404 /error.php
RewriteEngine On
RewriteRule ^([^/]*)-([^/]*)$ /index.php?$1$2
RewriteCond %{HTTP_HOST} ^reflap\.com [NC]
RewriteRule (.*) http://www.reflap.com/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/?$ /index.php?$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)/?$ /index.php?$1&$2 [QSA,L]
RewriteRule ^/facebook-chat/ http://chat.facebook.com/ [P]
ErrorDocument 404 /error.php
You need to put specifics in your rewrite rules above your generics - the ordering you have means that you are being redirected to index.php?new-page because you are matching a more general pattern first.
Try this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^reflap\.com [NC]
RewriteRule (.*) http://www.reflap.com/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)-([^/]*)$ /index.php?$1$2 [QSA,L]
RewriteRule ^([^/]*)/([^/]*)$ /index.php?$1&$2 [QSA,L]
RewriteRule ^([^/]*)/([^/]*)/$ /index.php?$1&$2 [QSA,L]
RewriteRule ^/facebook-chat/ http://chat.facebook.com/ [P]
RewriteRule ^([^/]*)$ /index.php?$1 [QSA,L]
RewriteRule ^([^/]*)/$ /index.php?$1 [QSA,L]
ErrorDocument 404 /error.php
This should fix your problem - but there's a more general issue in this .htaccess - RewriteCond directives only apply to the RewriteRule immediately following them. They don't continue on to any other rule following. This means that all rules after the first one will apply regardless of if the file or directory really exists.
http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html#rewritecond