.htaccess in subdirectory always refers to root - apache

I'm facing a quite strange behaviour of my .htaccess. Whenever I try to access a link rewritten by mod_rewrite, my .htaccess is refering to the root and not the subdirectory I'm working in. My folder structure looks like that:
htdocs/
blog6/
.htaccess
My .htaccess looks like that:
Options +FollowSymLinks -MultiViews
ErrorDocument 401 /core/error/401.php
ErrorDocument 403 /core/error/403.php
ErrorDocument 404 /core/error/404.php
ErrorDocument 500 /core/error/500.html
RewriteEngine on
RewriteBase /blog6/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)$ /index.php?page=$1 [L]
But whenever I try to access a file through a rewritten url, I get a 404 error and the log says:
C:/xampp/htdocs/index.php' not found or unable to stat
So it seems like my .htaccess wants to access the file in the htdocs instead of using the subdirectory. When I write /blog6 in my rewriteRule, everything works fine, but why RewriteBase isn't working properly? If it's important, I'm using <base> in my html

RewriteBase works only if you provide relative URL in target of RewriteRule so change your code to:
Options +FollowSymLinks -MultiViews
ErrorDocument 401 /core/error/401.php
ErrorDocument 403 /core/error/403.php
ErrorDocument 404 /core/error/404.php
ErrorDocument 500 /core/error/500.html
RewriteEngine on
RewriteBase /blog6/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ index.php?page=$1 [L,QSA]

Related

Rewrite URLs using .htaccess to a subfolder

my problem is that I moved my entire website to a subfolder of the public-html one. Tried to rewrite the URLS to automatically add the subfolder via the .htaccess but didn't succeed
this is the code :
ErrorDocument 401 "Unauthorized"
ErrorDocument 403 "Forbidden"
RewriteEngine On
RewriteBase /
DirectoryIndex index.php index.cgi index.html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ anb/$1 [L,QSA]
Thanks in advance
I got it to work by deleting the line
RewriteBase /
Apparently the RewriteBase doesn't work well when the .htaccess is placed in the root directory A.K.A [public_html]

Apache - .htaccess: mod_rewrite and ErrorDocument won't work together

I'm not sure what the mistake is here, but I'm not able to redirect 404 errors to a specific page. This is how the .htaccess looks like:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+) - [PT,L]
RewriteRule ^(.+) index.php
ErrorDocument 404 http://www.someurl.com/error404/404.php
The error folder and file exist, and when I do a curl -i http://www.someurl.com/notexistent it returns a correct 404 error code. But no redirection, neither curl, nor in the browser. How is that possible?
Sorry, I'm no expert with these .htaccess configurations, help is appreciated.
EDIT 1:
I tried different url's like:
ErrorDocument 404 http://www.someurl.com/error404/
ErrorDocument 404 /error404/404.php
ErrorDocument 404 /error404/
But withouth success.
EDIT 2:
I forgot to mention: mod_rewrite works as expected. Only the 404 redirects do not work.
With your RewriteRule you rewrite all the pages without real file or folder to index.php no exceptions for 404 errors...
Try with:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php? [L,R=404]

Force Apache to send 200 instead of 404 in .htaccess file

I need to force Apache to send 200 OK instead of 404 in .htaccess I don't know is this possible. I have found below text from one post but it doesn't work I don't know why...
From: http://www.askapache.com/htaccess/apache-status-code-headers-errordocument.html
Create a blank file in your main web directory named 404.. can be blank.
Add this to your .htaccess file:
Redirect 200 /404
ErrorDocument 404 /404
That will change the Apache ErrorDocument to the /404 file. But the Redirect line causes requests for /404 to issue a 200 OK response instead of a 404.
If that doesn't work it is due to rewrites no doubt. So if that's the case add this under those first 2 lines but above any other rewrite lines in your .htaccess file:
Options FollowSymLinks ExecCGI
RewriteEngine On
RewriteBase /
# If the requested file doesnt exist
# and if the requested file is not an existing directory
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^[^4]* /404 [L,S=4000]
Explained in more detail at: http://www.askapache.com/htaccess/apache-status-code-headers-errordocument.html#Automate_ErrorDocument_Triggering
In order to send 200 status instead of usual 404 you can use this rewrite rule:
ErrorDocument 404 default
RewriteEngine On
RewriteBase /
RewriteRule ^404\.html/?$ - [L]
# If the requested file doesnt exist
# and if the requested file is not an existing directory
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . 404.html [L]

How do I avoid a 500 error with .php hiding using mod_rewrite?

I have got this in my .htaccess file:
ErrorDocument 403 /403.php
ErrorDocument 404 /404.php
Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.php [L]
When I try to access a page or directory that isn't there, I get 'Error 500 - internal server error'. I am using GoDaddy, if it is relevant.

htaccess root/subfolder and root/subfolder/ into root/subfolder2/subfolder/

I want to be able to enter the following into the URL:
https://mydomain.com/fork
or
https://mydomain.com/fork/
And for both to goto a subfolder called
https://mydomain.com/prod/fork_contents/index.html
But still only display https://mydomain.com/fork (or https://mydomain.com/fork/, either way)
I would like index.html to act as the root so all the image urls, etc still work (they are relative to '/prod/fork_contents/')
Currently the .htaccess in the root is:
ErrorDocument 404 /live/site_documents/document404.html
ErrorDocument 403 /live/site_documents/accessDenied.html
Options +FollowSymLinks
Options -Indexes
DirectoryIndex index.php
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d [OR]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^fork$ /fork/index.html [L,QSA]
RewriteRule ^fork/$ /fork/index.html [L,QSA]
RewriteRule ^fork/(.*)$ /prod/forklift_contents/$1 [L,QSA]
https://mydomain.com/fork/ Works fine!
https://mydomain.com/fork Doesn't work. The index.html loads, but it can't find any of the files. Firebug's net panel reports the following 'https://mydomain.com/css/style.css not found'. So the second case (without the trailing /) doesn't seem to know to go into https://mydomain.com/fork/css/style.css.
Thoughts?
You can use the question mark for optional charters
RewriteRule ^fork\/? /prod/fork/$1 [L]
For a very good tutorial on rewriting check this website
.htaccess tips and tricks..