htaccess issue when i try to access a directory - .htaccess

So here's my code:
RewriteEngine On
RewriteRule ^contact$ contact.php [QSA,L]
RewriteRule ^error$ error.php [QSA,L]
RewriteRule ^([^/.]+)/?$ product.php?weblink=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . /error [L,R=301]
It's simple so when I try to access host/product-name it works but when I try to access for example a directory /blog or /blog/ redirect pe to error page specified by last rule. What's the problem?

You can do it this way
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule . - [L]
RewriteRule ^contact$ contact.php [QSA,L]
RewriteRule ^error$ error.php [QSA,L]
RewriteRule ^([^/.]+)/?$ product.php?weblink=$1 [L,QSA]
RewriteRule . /error [L,R=301]

Related

404 Error is given for the following htaccess Code

RewriteEngine On
<IfModule mod_rewrite.c>
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\.elancemart\.com [NC]
RewriteRule (.*) https://elancemart.com/$1 [L,R=301]
</IfModule>
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php
RewriteRule ^services/([a-zA-Z0-9-]+) service_details.php?slug=$1 [NC,L]
RewriteRule ^category/([a-zA-Z-]+) category.php?catSlug=$1 [NC,L]
RewriteRule ^blog/([0-9]+)/([a-zA-Z0-9-]+) blog/article.php?id=$1&&slug=$2 [NC,L]
RewriteRule ^verify/([a-zA-Z0-9-]+) verify.php?verify=$1 [NC,L]
RewriteRule ^services services.php
RewriteRule ^services/page/([\d])$ /services.php?page=$1 [NC,L,QSA]
RewriteRule ^funds add-funds.php [NC,L]
RewriteRule ^sitemap\.xml/?$ sitemap.php
RewriteRule ^article-sitemap\.xml/?$ article-sitemap.php
ErrorDocument 404 /index.php
I am getting 404 error when hitting services/page/([\d+])$ /services.php?page=$1
The file exists, though When I change the rewrite rule to service/page/([\d+])$ it is working fine, I need services in the URL, not service.
With your shown samples, please try following htaccess rules in your htaccess file. I have put 3 of services rules one after another to make it easy looking.
Also make sure your htaccess file is present along side with servcies.php file.
Please make sure to clear your browser cache before testing your URLs.
RewriteEngine On
<IfModule mod_rewrite.c>
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\.elancemart\.com [NC]
RewriteRule ^(.*)/?$ https://elancemart.com/$1 [L,R=301]
</IfModule>
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)/?$ $1.php [L]
RewriteRule ^services/(\w+)/?$ service_details.php?slug=$1 [NC,L]
RewriteRule ^services/?$ services.php [NC,L]
RewriteRule ^services/page/(\d+)/?$ services.php?page=$1 [NC,L,QSA]
RewriteRule ^category/([a-zA-Z-]+)/?$ category.php?catSlug=$1 [NC,L]
RewriteRule ^blog/([0-9]+)/(\w+)/?$ blog/article.php?id=$1&&slug=$2 [NC,L]
RewriteRule ^verify/(\w+)/?$ verify.php?verify=$1 [NC,L]
RewriteRule ^funds/?$ add-funds.php [NC,L]
RewriteRule ^sitemap.xml/?$ sitemap.php [NC,L]
RewriteRule ^article-sitemap.xml/?$ article-sitemap.php [NC,L]
ErrorDocument 404 /index.php

htaccess for restful apis with sub directories not working

I have a directory structure for my apis as:
api/
cameras/
index.php
...
.htaccess
bar/
set/
index.php
scan/
index.php
retrieve/
index.php
list/
index.php
Before I added the bar folder I had this working with my .htaccess folder
RewriteEngine On
RewriteCond %{HTTP:Authorization} ^(.*)
RewriteRule ^(.*) - [E=HTTP_AUTHORIZATION:%1]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php [L]
I then added this to make it work.
RewriteEngine On
RewriteCond %{HTTP:Authorization} ^(.*)
RewriteRule ^(.*) - [E=HTTP_AUTHORIZATION:%1]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule bar/set/(.*) bar/set/index.php [L]
RewriteRule bar/scan/(.*) bar/scan/index.php [L]
RewriteRule bar/list/(.*) bar/list/index.php [L]
RewriteRule bar/retrieve/(.*) bar/retrieve/index.php [L]
RewriteRule (.*) index.php [L]
They all work except the first one in the list set. What have I done wrong?
I think the order of your conditions might be causing an issue. Without seeing the URL's you are using I would have to assume. However RewriteCond only applies to the rule directly following it. You put the new rules between the conditions and your original rule. Change the order and try it this way and see how it works.
RewriteEngine On
RewriteCond %{HTTP:Authorization} ^(.*)
RewriteRule ^(.*) - [E=HTTP_AUTHORIZATION:%1]
RewriteRule ^bar/set/(.*)$ bar/set/index.php [L]
RewriteRule ^bar/scan/(.*)$ bar/scan/index.php [L]
RewriteRule ^bar/list/(.*)$ bar/list/index.php [L]
RewriteRule ^bar/retrieve/(.*)$ bar/retrieve/index.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) index.php [L]
RewriteCond only gets applied to next RewriteRule. Try this code instead:
DirectoryIndex index.php
RewriteEngine On
RewriteCond %{HTTP:Authorization} ^(.*)
RewriteRule ^ - [E=HTTP_AUTHORIZATION:%1]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^(bar)/(scan|set|scan|retrieve)/(.*)$ $1/$2/index.php [L,NC]
RewriteRule . index.php [L]

.htaccess redirect not found page to a specific page

Here's my code
RewriteRule ^error$ error.php [QSA,L]
RewriteRule ^siteuri_partenere$ siteuri_partenere.php [QSA,L]
RewriteRule ^politica_de_confidentialitate$ politica_de_confidentialitate.php [QSA,L]
RewriteRule ^blog - [L,NC]
RewriteRule ^([^/.]+)/?$ game.php?weblink=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . /error [L,R=302]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
So, if i change this line
RewriteRule . /error [L,R=302]
to
RewriteRule . /page_not_found [L,R=302]
and then i test www.domain.com/1q2w3e, is still redirect to /error and not to /page_not_found so i'm very confusing where is the problem?
Most likely you have some ErrorDocument 404 directive that is overriding your rewrite rules. Place this code in your root .htaccess:
Options +FollowSymLinks -MultiViews
ErrorDocument 404 default
RewriteEngine On
RewriteRule ^blog - [L,NC]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteRule ^error$ error.php [L]
RewriteRule ^siteuri_partenere$ siteuri_partenere.php [L]
RewriteRule ^politica_de_confidentialitate$ politica_de_confidentialitate.php [L]
RewriteRule ^([^/.]+?)/?$ game.php?weblink=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /page_not_found [L,R=302]

.htaccess redirect index.php to /

I would like to hide the index.php page and just show the domain.
Is this possible with .htaccess?
RewriteRule ^index\.php/?$ / [L,R=301,NC]
Also tried:
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9} /index.php HTTP/
RewriteRule ^index.php$ http://example.com/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
index.php still shows
Try, It works for me! Make sure your have AllowOverride All set in httpd.conf
RewriteEngine On
RewriteCond %{REQUEST_URI} index\.php
RewriteRule ^(.*)index\.php$ /$1/ [R=301,L]
There is a regex issue in your rules, I have modified your rules and it works for me:
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} index\.php
RewriteRule ^index\.php$ http://example\.com/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index\.php [L]
RewriteRule ^(.*)index\.(html|php)$ http://%{HTTP_HOST}/$1 [R=301,L]
You can rewrite '/index.php' through .htaccess like this:
# Remove /index.php from all urls
RewriteRule ^(.+)/index\.php$ /$1 [R=302,L]

don't rewrite static css/js/img files

I'm trying to get my htaccess file not to rewrite my static files (js/css/images).
This is my current htaccess file:
RewriteEngine on
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule !\.(jpg|css|js|gif|png)$ public/ [L]
RewriteRule !\.(jpg|css|js|gif|png)$ public/index.php?url=$1
How do I rewrite it?
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^.*\.(jpg|css|js|gif|png)$ [NC]
RewriteRule ^(.*)$ public/index.php?url=$1
All requests to not existing files which doesn't end with listed extensions (case nonsensitive match) are rewritten to public/index.php passing the current URL as url= GET argument
This only works for the one line below it. So if you have many RewriteRules it could help to use something like this:
RewriteEngine on
RewriteRule ^(.*?)\.(php|css|js|jpg|jpeg|png|pdf)$ - [L]
RewriteRule ^(.+)/(.+)/?$ index.php?page=$1&subpage=$2 [L]
RewriteRule ^(.+)$ index.php?page=$1 [L]
for posterity.
Maybe my rules will be useful
\www\.htaccess
#file: \www\.htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} \.(css|js|html?|png|gif|jpg|jpe?g|svg|xls.?|pdf|docx?|eot|ttf|woff2?)$ [NC]
RewriteRule (.*\.[css|js|html?|png|gif|jpg|jpe?g|svg|xls.?|pdf|docx?|eot|ttf|woff2?]+) public/$1?a [QSA,L]
RewriteCond %{REQUEST_URI} \.(css|js|html?|png|gif|jpg|jpe?g|svg|xls.?|pdf|docx?|eot|ttf|woff2?) [NC]
RewriteRule (.*\.[css|js|html?|png|gif|jpg|jpe?g|svg|xls.?|pdf|docx?|eot|ttf|woff2?]+) public/$1 [QSA,L]
#RUN TEST
RewriteCond %{REQUEST_URI} !\.(css|js|html?|png|gif|jpe?g|svg|xls.?|pdf|docx?|eot|ttf|woff2?)$ [NC]
RewriteRule ^(.*)$ index.php?REQUEST_FILENAME=%{REQUEST_FILENAME}&date=$1&REQUEST_URI=%{REQUEST_URI} [QSA,L]
#END TEST
RewriteRule . index.php?url=%{REQUEST_URI} [QSA,L]
ErrorDocument 404 index.php?error=404
\www\index.php
<?php
// file: /www/index.php
var_dump($_GET);
\www\app\.htaccess
#file: \www\app\.htaccess`
RewriteEngine On
RewriteRule ^(.*)$ ../_%{REQUEST_URI} [QSA,L]
\www\public\.htaccess
#file: \www\public\.htacces`
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=%{REQUEST_URI}&rewrite=$1 [QSA,L]
/www/public/index.php
<?php
// file: /www/public/index.php
var_dump($_GET, __DIR__);

Resources