Rewrite URLs using .htaccess to a subfolder - apache

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]

Related

.htaccess redirecting sub domains to sub domains name folder

My site structure is as follows:
/
static/
www/
index.html
img/
www2/
index.html
img/
www3/
index.html
img/
I have this .htaccess that does the following:
According to a subdomain for instance www.domain.com/index.html i redirect to the right subdomain folder which is /www/index.html.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/static/*
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /%1/$1
</IfModule>
It works perfectly when i am accessing the site with www.domain.com/index.html
but not without index.html meaning
www.domain.com/ .
I have tried many condition but non of them seems to work , and if it works it breaks what i have now.
Which condition should i add for redirecting the www.domain.com/ to /www/index.html ?
Thanks
EDIT
The error i am receiving is:
"Forbidden You don't have permission to access / on this server."
meaning i am redirected to the / folder which doesnt contains an index.html.
To me it sounds like you don't have a directory index set. There is nothing wrong with going directly to the file but apache doesn't consider index.html your index file. And with directory listing turned off you will get that error. You don't need rewrite for that. Use the directive. That should prevent the 403 Forbidden.
DirectoryIndex index.html
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/static/*
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /%1/$1
</IfModule>

.htaccess in subdirectory always refers to root

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]

htaccess RewriteRule not working - (404 error) from server

I'm trying to rewrite my URL from this:
http://www.example.com/admin/index.php?id=title
to:
http://www.example.com/admin/title
I'm using this code in my htaccess:
RewriteEngine On
RewriteRule ^([^/]*)\.html$ /admin/index.php?id=$1 [L]
But then when I try out rewritten URL's i get a 404 error from my server. What is the mistake I'm making? The .htaccess is in a subfolder called admin and the rewrite rule should only work for that folder.
This .htaccess should be placed inside the folder admin that must be inside your root folder:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /admin/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)$ index.php?id=$1 [L]
The 2 conditions make sure we are not redirecting an existent file or folder and rule tells we want to extract anything not a / and use as the ID.
The RewriteBase tell us your parent folder is admin and as such we work from there and onwards.

Redirect of all files to index.php in a subdirectory

I have a website under /var/www/v1 and /var/www/v2. In the v2 folder I have a .htaccess file with the following code:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /index.php?request=$1
The rewrite rule wont work under the v2 folder, but works fine if I put it and the files into the /var/www/ directory.
For example I have these files:
upload.php
display.php
index.php
When I got to website.com/v2/display/ it throws a 404 saying "The requested URL /v2/display/ was not found on this server."
You can try to disactivate MultiViews:
Options -MultiViews
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /v2/index.php?request=$1 [L]

.htaccess and redirecting nonexistent files and directories

I am trying to figure out how to program a .htaccess file to redirect any file or directory that does not exist to the index.html file in the web root.
I sort of figured it out with this directive:
ErrorDocument 404 /index.html
The problem is that an HTTP 404 error is still returned by the web server. I'd like to show a permanent redirect for all these files instead. It is important that a 404 error NOT be returned.
Suggestions appreciated!
Try adding this rule to your htaccess file instead:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^ /index.html [L,R=301]
This checks that the request isn't a file, isn't a directory, and isn't a symlink. Then redirects whatever the request is to /index.html.
There is a convinient FallbackResource directive in Apache
FallbackResource /index.html