I have the following folder structure:
www-root
Backend
Config
Etc
Frontend
Administration
StoreFront
I would like to be able to access the directories from the main url and hiding the subdirectories in between.
So the administrative part I should be able to access like this:
http://localhost/Administration/
The main page which is stored in the subdirectory "StoreFront", I want to be able to access from the root:
http://localhost
This is the code in my .htaccess file so far:
# Store Redirect
RewriteEngine on
RewriteCond %{HTTP_HOST} ^localhost [NC]
RewriteCond %{REQUEST_URI} !^/Frontend/StoreFront
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /Frontend/StoreFront/$1
RewriteCond %{HTTP_HOST} ^localhost [NC]
RewriteRule ^(/)?$ /Frontend/StoreFront/index.php [L]
RewriteCond %{REQUEST_URI} !^/Administration
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /Frontend/Administration/$2
This code however does not work correctly. It rewrites every file except the index.php file to the Administration subdirectory. One side note: php files which are in the backend directory should remain "includable" from the frontend.
Let me tell upfront you that what you're trying to achieve is mission impossible, now let me tell you why. You have this rule:
RewriteRule ^(.*)$ /Frontend/StoreFront/$1
and down further you have:
RewriteRule ^(.*)$ /Frontend/Administration/$2
You cannot have .* going to both the places. You need to distinguish these 2 paths somehow.
It is besides the point that you have other problems also e.g.:
Not using L (LAST) flag wherever needed
Using $2 instead of $1 in 2nd rule
EDIT: Based on your comments:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule ^(Administration(?:/.*|))$ /Frontend/$1 [L,NC]
RewriteCond %{HTTP_HOST} ^localhost [NC]
RewriteRule ^$ /Frontend/StoreFront/index.php [L]
RewriteCond %{HTTP_HOST} ^localhost [NC]
RewriteCond %{REQUEST_URI} !^/Frontend/StoreFront
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /Frontend/StoreFront/$1 [L]
Related
I'm trying to dispatch all traffic in multiple directories based on a specific keyword.
I have the following directory structure:
dotcom/
dotcom/directory1/ (with subdirs)
dotcom/directory2/ (with subdirs)
dotcom/directory3/ (with subdirs)
I have a .htaccess file located in dotcom and I would like to redirect everything behind each directory to an index file in each directory.
Example:
dotcom/directory1/anything/blabla to dotcom/directory1/index.php
dotcom/directory2/anything/blabla to dotcom/directory2/index.php
dotcom/anythingNotExisting to dotcom/index.php
Anything not in one of the existing directories should be redirected to dotcom/index.php
I tried the following for dotcom:
RewriteEngine On
RewriteCond %{ENV:HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
This catches everything
But when I tried to add conditions like the following, I get a 404:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/directory1/(.*)$ directory1/index.php?path=$1 [NC,L,QSA]
With this, if I try to access dotcom/directory1/blabla I have a 404 while if I access dotcom/directory1/ it goes to the right index.php
I have tried to use the full path dotcom/directory1/ but it doesn't help.
You may use these rules inside dotcom/.htaccess:
DirectoryIndex index.php
RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
# ignore all rules below this for real files and directories
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
# for URIs starting with know directory paths
RewriteRule ^(directory1|directory2)/(.*)$ $1/index.php?path=$2 [NC,L,QSA]
# everything else
RewriteRule .+ index.php?path=$0 [L,QSA]
I have found something that works with the following:
RewriteEngine ON
RewriteCond HTTPS off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^directory1/(.*)$ /dotcom/directory1/index.php?path=$1 [NC,L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^directory2/(.*)$ /dotcom/directory2/index.php?path=$1 [NC,L,QSA]
RewriteEngine ON
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /dotcom/index.php?path=$1 [NC,L,QSA]
This way I'm catching everything from /directoryX/ and redirect it to the root of the directory, everything else go to dotcom
I've been trying several solutions in the internet and can't make the redirect voodoo to work.
Setup
My setup is a website build with jekyll, and I want to consume it as mydomain.com/~user.
The folder structure is
~user
- _site
- index.html
- folder1
- index.html
- folder2
- index.html
- other_stuff
Problem
I was able to redirect from mydomain.com/~user to mydomain.com/~user/_site. So, half way through. The problem came when I tried to mask and remove the _site from the URL again. And I hit a wall.
Basically I want to use: mydomain.com/~user and see that URL in the browser, while beeing served the page that lives in mydomain.com/~user/_site/index.html. And same goes for other links
Request See Served
======= ======= =======
mydomain.com/~user mydomain.com/~user mydomain.com/~user/_site/
mydomain.com/~user/folder1 mydomain.com/~user/folder1 mydomain.com/~user/_site/folder1
What I try
RewriteEngine On
RewriteCond %{HTTP_HOST} ^mydomain\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/~user/_site/?
RewriteRule ^(.*)$ /~user/_site/$1
RewriteEngine On
RewriteCond %{HTTP_HOST} ^mydomain\.com$
RewriteRule !^~user/_site(.*)$ /~user/_site$1 [L]
RewriteEngine On
RewriteCond %{HTTP_HOST} ^mydomain\.com$
RewriteRule ^~user/_site(.*)$ /~user$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^mydomain\.com$
RewriteRule !^~user/_site(.*)$ /~user/_site$1 [L]
However, all these
Do not end with the desired hidden URL in the bar
Give an error, as some of the redirects are not allowed in the server
Partial solution
After tweaking the proposed solution, I was able to identify one variation that partially works
RewriteEngine On
RewriteBase /~user
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/_site/? [NC]
RewriteRule ^(.*)$ _site/$1 [L]
Note that in the last two line, i remove the ~user from the regex. This redirects all subfolders and pages, and allows other folders that are in ~user to be accessed.
However, the "home" mydomain.com/~user is the only one that is not redirected. As it exists the RewriteCond %{REQUEST_FILENAME} !-d doesn't allow it to be processed.
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.com$ [NC]
RewriteCond %{REQUEST_URI} ^/$ [NC]
RewriteRule ^/?$ _site/ [L]
To redirect the home (~user) to mydomain/~user/_site, but it doesn't work. And tried variations of ^/?$ and ^$. But they don't work either.
Ideas?
You should try it this way.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/~user/_site/? [NC]
RewriteRule ^~user/(.*)$ /~user/_site/$1 [L]
We have a rewrite rule that looks like this:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule (?!^(cms|images|js|mobile)(/.*|)$)^.*$ page.php [NC,L]
Which works as expected; if the file does not exist, forward to page.php which is a generic page and allows for pages with custom URLs.
We also want to force the www. to precede the domain: example.com -> www.example.com
For that, we use:
RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
This also works, but when the two are supposed to work together:
example.com/non-existent-file
We end up getting:
www.example.bom/page.php
Which defaults to our 404 page. How do I modify these rules to work together? I have tried playing with some of the different suffic characters (ie. removing the L in the [NC,L] in an attempt to get both rules to process).
I have also tried repeating the forward to page.php lines beneath the www redirect (and removing the L suffix) with no success.
The entire HTACCESS file looks like:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule (?!^(cms|images|js|mobile)(/.*|)$)^.*$ page.php [NC,L]
RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule ^(.*)$ http://www.example.com/$1 [R=302,L]
Also, is there a %{CONSTANT} I can use in place of example.com that represents the current domain?
The constant you're looking for is %{HTTP_HOST} as provided by the HTTP request. Perhaps you are looking for the variable %{SERVER_NAME} for the virtual host's default servername?
Place the hostname checking rule first, and use a RewriteCond in the page.php rule to only apply if the domain is already correct:
RewriteEngine On
# First rewrite the domain
RewriteCond %{HTTP_HOST} ^example\.com$
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
# Then rewrite the files
# At this point, the domain should already have been enforced
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule (?!^(cms|images|js|mobile)(/.*|)$)^.*$ page.php [NC,L]
I would like mod_rewrite to redirect all requests to non existing files and folders, and all requests to the main folder ("root") to a subfolder. So i've set it up like this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteCond %{REQUEST_FILENAME} !-d [NC,OR]
RewriteCond %{REQUEST_URI} / [NC]
RewriteRule ^(.*)$ /my/subfolder/$1 [L,QSA]
Unfortunately, it does not work: if i request example.com/public/ it redirects to my processing script (so redirecting to my/subfolder/index.php?app=public ) although the folder "public" exists.
Note that requesting domain.com/ correctly redirects to my/subfolder/index.php
Why is that?
it does not work because your last condition is not matching only the root but any uri that has / in it, which is basically everything. Try the following instead:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d [OR]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^(.*)$ /my/subfolder/$1 [L,QSA]
Note that [NC] is not needed as you are not trying to match any alphabets, so "No Case" is not really needed.
Hope it helps.
How can I redirect requests to mydomain.tld/somepage.ext to mydomain.tld/mydomain/somepage.ext? I have subdomains like subdomain.mydomain.tld that I don't want to be affected by this.
I can't seem to get it to work right. I'm trying this:
RewriteEngine on
RewriteCond %{HTTP_HOST} mysite.com
RewriteCond %{REQUEST_URI} !^/mysite
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) /mysite/$1
But it isn't redirecting anything at all.
I also want to exclude one or two folders from this rule.
This seems to be working...
RewriteEngine on
RewriteCond %{HTTP_HOST} mysite\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/mysite/ # but I don't see why this line is needed when I've used [L] below
RewriteCond %{REQUEST_URI} !^/ignored_folder/
RewriteRule .* /mysite/$0 [QSA,L]
At least for now.