For a website, I want to set up a redirection using .htaccess.
My folder structure is something like
/
/folderA/
/folderB/
/index/
where folderA and B and index contain subfolders and files. Now, I want to rewrite all requests for the root / and for all not existing folders and files to index. It should be masked. It seems to me like an easy task but I cannot get it to work. So far, I tried
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) /index/$1 [L]
The redirection somehow works but it does not work when I call the root http://example.org/ directly.
Is the root also seen as valid directory or excluded in the RewriteCond checks? Any ideas how to realize that?
Yes, the root is also a directory. You will need to add another rule to rewrite the root only. For example:
RewriteRule ^$ /index/ [L]
And since the root is a directory, you might as well exclude this from the first rule. ie. Change (.*) to (.+).
HOWEVER, your existing rule will result in a rewrite-loop (500 error) if the URL you are rewriting to in /index/... does not exist either*1. eg. If you request /foo, it rewrites to /index/foo and if /index/foo does not exist then it rewrites to /index/index/foo to /index/index/index/foo etc.
You will need to add an additional condition (or use a negative lookahead) to prevent requests to /index/... itself being rewritten.
(*1 Unless you have another .htaccess file in the /index subdirectory that contains mod_rewrite directives and you have not enabled mod_rewrite inheritance.)
For example:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/index/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.+) /index/$1 [L]
RewriteRule ^$ /index/ [L]
Related
I have some folders in the root dir with an .htaccess file inside to deny for any access(deny from all). And my htaccess in root is:
RewriteEngine On
Options -Indexes
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
It's working fine except when I get the folders name in url. I want to ignore folders name and just redirect any url to index.php?url.
Could anyone help?
The !-d, !-f, !-l conditions mean only apply the rewrite rule if the access url does not resolve to an existing file, directory or link. So if the directory exists, it won't apply the rule. You need to remove those. You then need to prevent the recursive rewrite of index.php to itself like so :
RewriteRule ^index.php$ - [L]
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
You probably want to also change .+ in the last rule to .* to also match the empty url.
Indeed it seems the inner .htaccess files are parsed first, before the RewriteEngine one. So you will need to remove those .htaccess and rely on the rewrite rule.
I want to point all subfolders and non-existent files to root folder.
www.domain.com/folder/david to www.domain.com/folder/
or
www.domain.com/folder/david/ to www.domain.com/folder/
You can try this in your /.htaccess file:
RewriteEngine on
# Make sure the requested path does not exist (file or folder):
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# If it doesn't exist, redirect it to `/folder/`
RewriteRule ^folder/(.*)/?$ /folder/? [NC,R,L]
The NC flag makes the rule case-insensitive. If you do not need it, you can remove it.
If you would like to make the redirect permanent, exchange the R flag for R=301.
If it doesn't matter whether or not it exists, use the following instead:
RewriteEngine on
# Redirect everything to `/folder/`
RewriteRule ^folder/(.*)/?$ /folder/? [NC,R,L]
Update
Based on your comments, it appears you are running an application in /folder/. As such, it would be better to place your .htaccess file in /folder/, and fill it with the following:
RewriteEngine on
RewriteBase /folder/
# Make sure the requested path does not exist (file or folder):
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# If it doesn't exist, rewrite it to index.php
RewriteRule ^ index.php [NC,L]
You then do not need an .htaccess file inside the root - only the one in /folder/ is needed.
In my "public_html" directory I have the following structure:
- root
- index.html
- blog
- index.html
- lab
- index.html
- wp
- (WORDPRESS FILES)
The "lab" and "wp" directories are just subdomain directories ("http://lab.tomblanchard.co.uk" and "http://wp.tomblanchard.co.uk") which work fine.
Basically I want the main domain ("http://tomblanchard.co.uk") to point to the "root" directory without any actual redirecting, for example, I want "http://tomblanchard.co.uk" to point to the "index.html" file within the "root" directory, I want "http://tomblanchard.co.uk/blog" to point to the "index.html" file within the "root/blog" directory and so on.
I have kind of achieved this with the following code in my ".htaccess" file:
# Add directives
RewriteEngine on
# Remove ".html" extension from URLs
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
# Change root directory to "root" folder
Options +FollowSymLinks
RewriteCond %{REQUEST_URI} !(.*)root
RewriteRule ^(.*)$ root/$1 [L]
The only problem is that things like "http://tomblanchard.co.uk/root/" and "http://tomblanchard.co.uk/root/blog/" still work when really they shouldn't even be able to be accessed (404).
If anyone has any idea on how to sort this or has a stronger method of doing this it would be greatly appreciated.
Update
Finally got it working how I wanted it after hours of researching, I used the following:
# Add directives
RewriteEngine on
# Change root directory to "root" folder
RewriteCond %{THE_REQUEST} ^GET\ /root/
RewriteRule ^root/(.*) /$1 [L,R=301]
RewriteRule !^root/ root%{REQUEST_URI} [L]
The order of directives in mod_rewrite is important, as each rule sees the output of the previous rule as its input to test. You need to do 3 (or possibly 4) things, in order:
Deny access to any URL beginning /root/ (we have to do this first, else everything will be denied!)
It's generally good practice to ensure each URL has only one valid form, so URLs which do specify .html should cause a browser redirect to the non-.html form. This needs to happen before other rewrites, otherwise you can't tell the difference between a .html from the browser and one you've added virtually.
Look up any URL not denied above in the /root/ directory, rather than the configured DocumentRoot
Look up any URL not pointing at a directory under the URL + .html, if that file exists. This has to come after other rewrites, or the "file exists" check will always fail.
# General directives
Options +FollowSymLinks
RewriteEngine on
# Deny URLs beginning /root/, faking them as a 404 Not Found
RewriteRule ^root/ [R=404]
# Additional rule to strip .html off URLs in the browser
RewriteRule ^(.*)\.html$ $1 [R=permanent,L]
# Rewrite everything remaining to the /root sub-directory
# (Host condition was in your post originally, then edited out; this is where it would go)
RewriteCond %{HTTP_HOST} ^(www\.)?tomblanchard\.co\.uk$
RewriteRule ^(.*)$ root/$1
# Handle "missing" ".html" extension from URLs
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
PS: Note my careful language to describe (internal) rewrites, as opposed to (browser) redirects: the rule you have is not removing .html from anything, it is adding it, thus allowing the page to be accessed if someone else removes it. Since you are often modifying both within a set of rules, it's important to keep clear in your head the distinction between the URL the browser has requested, and the virtual URL Apache will ultimately serve.
You are not defining any rule to block /root address so how do you want to block it when there is nothing to do that?
Try this:
# Add directives
RewriteEngine on
RewriteCond %{REQUEST_URI} .root [NC]
RewriteRule (.*) / [L,R=404]
# Remove ".html" extension from URLs
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
# Change root directory to "root" folder
RewriteCond %{HTTP_HOST} ^tomblanchard.co.uk$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.tomblanchard.co.uk$
RewriteCond %{REQUEST_URI} !.root
RewriteRule (.*) /root/$1 [L,R=301,QSA]
This is not tested so if it wouldn't work, play around with it to get your need.
I have a website running at localhost/pm and the RewriteBase is correctly set to /pm/. There is a link tag in my document: <link ... href="themes/default/css/default.css">.
When the url is localhost/pm or localhost/pm/foo the CSS works all right. When there are more slashes in the URL, however, like localhost/pm/foo/bar the relative URL if the stylesheet changes to foo/themes/default/css/default.css.
How do I get this to work without having to put some sort of PHP path resolution in the link tag?
# invoke rewrite engine
RewriteEngine On
RewriteBase /pm/
# Protect application and system files from being viewed
RewriteRule ^(?:system)\b.* index.php/$0 [L]
# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite all other URLs to index.php/URL
RewriteRule .* index.php/$0 [PT]
EDIT:
Basically what I need now is this:
If request contains folder name /themes/ scrap everything that is before /themes/ and rewrite the rest to /pm/themes/...
I tried it like this: RewriteRule ^.*(/themes/.*)$ /pm/themes/$1 but I get an internal server error. Why?
If I do it like this: RewriteRule ^.*(/themes/.*)$ /pm/themes/ (ie. just remove $1 from the end) and use the URL http://localhost/pm/foo/themes/foo/ the resulting physical location is http://localhost/pm/themes which is what is expected too, which in turn means that at least my regex is correct. What am I missing?
The RewriteRule is almost correct
RewriteRule ^.*(/themes/.*)$ /pm/themes/$1
This rewrites http://localhost/pm/foo/themes/default/css/default.css to http://localhost/pm/themes/themes/default/css/default.css, which is one themes too much. Use this instead
RewriteRule /themes/(.*)$ /pm/themes/$1 [L]
But now you have an endless rewrite loop, because /pm/themes/.. is rewritten again and again. To prevent this, you need a RewriteCond excluding /pm/themes
RewriteCond %{REQUEST_URI} !^/pm/themes/
RewriteRule /themes/(.*)$ /pm/themes/$1 [L]
Now the request is rewritten only once and you're done.
You probably need to add the following lines before your RewriteRule:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
It will only evaluate your rewrite rule if the requested file or directory doesn't exist.
You should post your .htaccess file so we can offer better advice
Is there way to use .htaccess as follow
mydomain.com
redirects to
mydomain.com/index.php
and
mydomain.com/everything except index.php
redirects to
mydomain.com/index.php?u=everything except index.php
In other words, convert everything (directories, sub-directories, pages and so on) to a simple string param to pass through the variable u to index.php
Any help will be appreciated.
To send absolutely everything to index.php:
RewriteEngine On
RewriteCond %{REQUEST_URI} !index.php
RewriteRule (.*) index.php?u=$1 [QSA,L]
Note that this would also send requests for images, css, javascript, etc to index.php. Since this probably isn't what you want, this one may be more appropriate:
RewriteEngine On
RewriteCond %{REQUEST_URI} !index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php?u=$1 [QSA,L]
This one would send all requests for files and directories that don't exist to index.php. Note this would mean that anything in your htdocs folder would be accessible by it's url. So this would rely on your htdocs folder being mostly empty.