I'm trying to edit my .htaccess file to do the following:
1) Determine if the HTTP_HOST is not my own domain, example.com
2) Determine if it is otherdomain.com/admin/_anything_here_
/admin/ is the specific folder I am looking for, then I don't care what comes after it (but I do need to keep it there). So basically anything that has /admin/ as the first folder and isn't example.com should rewrite to otherdomain.com/admin/_anything_still_here_
It's my understanding that using (.*)$ will "store" the _anything_here_ part and allow me to use it with $1.
I have this so far but it isn't working fully:
RewriteCond ^%{HTTP_HOST}+(/admin.*)$ !^([^.]+\.)*example\.com+(/admin.*)
RewriteRule (.*)$ rewrite/to/here$1/ [L]
Try adding the following to the .htaccess file in the root directory of your site.
You need to replace somefolder with the actual folder you want to rewrite to
RewriteEngine on
RewriteBase /
#if its not example.com
RewriteCond %{HTTP_HOST} !example\.com$ [NC]
#if its admin folder, rewrite it to somefolder
RewriteRule ^(admin/.*)$ somefolder/$1[L,NC]
The above assumes that otherdomain.com and example.com are on the same server with the same root folder.
Related
My domain is in my user folder. I would like to serve a file from another folder down a level, under the same user. I tried this:
RewriteRule ^test$ ../testfolder/test.txt [L]
But Apache serves a 400 Bad Request. Is htaccess capable of dropping down a level like this?
You may use this code to dynamically figure out PARENT directory and then rewrite to that:
RewriteEngine On
# dynamically figure out parent directory using composite RewriteCond
RewriteCond $0#%{REQUEST_URI} ^([^#]*)#(.*)/[^/]+/\1$
RewriteRule .* - [E=PARENT:%2]
RewriteRule ^test$ %{ENV:PARENT}/test.txt [L,NC]
RegEx Demo
We have drupal 8 site with a folder in docroot. Lets say its in a folder called micrositefolder. It contains a single index.html file.
Now let's say micrositefolder lives on fullsite.com. I dont want someone to access the microsite via fullsite.com/micrositefolder, but instead only accessible via mymicrosite.com
I have already achieved that with the following:
# Prevent access to the static site from non-static site hosts.
RewriteCond %{REQUEST_URI} ^/micrositefolder [NC]
RewriteCond %{HTTP_HOST} !^mymiscrosite
RewriteRule .* /index.php [L,R=301]
# Only serve the static site if host begins with mymiscrosite.
RewriteCond %{HTTP_HOST} ^mymiscrosite
# Don't loop anything targeting the actual mask directory, to allow
# for linked scripts, stylesheets etc in the static HTML
RewriteCond %{REQUEST_URI} !^/micrositefolder/
#Any requests that made it this far are served from the /micrositefolder/ directory
RewriteRule ^(.*)$ /micrositefolder/$1 [PT]
That works great. I can now visit mymicrosite.com and it serves me that index.html in that folder.
I now have to include another page on that microsite. The url would be mymicrosite.com/ronnie. I created a folder inside of micrositefolder called ronnie with another index.html in it.
When I try to go to that url (mymicrosite.com/ronnie) it is being rewritten to mymicrosite.com/micrositefolder/ronnie/ and I cannot figure out why. I am pretty sure it has to do with that last line in my code snippet, but I cannot figure out how to make it just be mymicrosite.com/ronnie
One thing to note is if I view the url via mymicrosite.com/ronnie/ it works, but if I dont include the slash at the end it redirects to mymicrosite.com/micrositefolder/ronnie
You can add this rule below your existing rules in site root .htacess:
# add a trailing slash to directories
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule [^/]$ %{REQUEST_URI}/ [L]
This will add a trailing slash if current request is pointing to a directory.
Problem with your proposed approach (in the answer) is that:
It will perform a trailing slash 301 redirect even if it is an invalid URI such as mymicrosite.com/qwerty111
For cases like mymicrosite.com/ronnie where /micrositefolder/ronnie is an actual directory, it will perform an extra 301 redirect before showing index.html
Adding an .htaccess inside the micrositefolder with the below seems to have solved my issue
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*[^/])$ /$1/ [L,R=301]
</IfModule>
Using Apache and .htaccess how can I do the following:
Redirect all urls to include a fake folder.
For example:
http://example.com/
http://example.com/example.php
http://example.com/folder/example.php
redirect to:
http://example.com/fakefolder/
http://example.com/fakefolder/example.php
http://example.com/fakefolder/folder/example.php
fakefolder does not actually exist in the server directories so I also need to point http://example.com/fakefolder/ to http://example.com/.
So basically if the user goes to http://example.com/ I want the url to show http://example.com/fakefolder/ but at the same time still point to http://example.com.
You can use in your htaccess:
RewriteEngine on
RewriteCond %{THE_REQUEST} !\s/+fakefolder/ [NC]
RewriteRule ^ /fakefolder%{REQUEST_URI} [R=301,NE,L]
RewriteRule ^fakefolder(/.*)$ $1 [NC,L]
I am new to the .htaccess file.
I want to make pretty URLs but the server always gives me 404 or 500 errors.
For example, I want to redirect
http://www.example.com/dir1
to
http://www.example.com/dir1/file1.html
without showing file1.html in the address bar.
I've tried
RedirectRule /dir1/$ /dir1/file1.html but the server says 404.
The .htaccess is in root.
What should I do?
Remember that .htaccess is per directory directive and Apache strips the current directory path (thus leading slash) from RewriteRule URI pattern.
You can use this rule in root .htaccess:
RewriteEngine On
RewriteBase /
RedirectRule ^dir1/?$ dir1/file1.html [L,NC]
OR use this rule in /dir1/.htaccess:
RewriteEngine On
RewriteBase /dir1/
RedirectRule ^/?$ file1.html [L]
This htaccess rule will do the trick.
RewriteEngine On
RewriteBase /
RedirectRule ^dir1/?$ dir1/file1.html [L]
For the record the terminating /? means that you to redirect links with /dir1 and links with /dir1/
I have two separate WordPress sites on the same domain, one installed in the following way:
http://subdomain1.domain.tld and http://subdomain2.domain.tld.
http://domain.tld redirects to subdomain1.
I need to remove all www. to non-www (this now works as tested on http://htaccess.madewithlove.be/), www.domain.com now returns domain.com but i am still getting a 404 page returned when i type in a subdomain prepended with www.
My current code, with the suggestions by #icabod now looks like this.
# 301 redirect www to non-www url
# including subdomains
<IfModule mod_rewrite.c>
Options +FollowSymLinks
Options +Indexes
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)
RewriteRule (.*) http://%1/$1 [R]
</IfModule>
mod_rewrite is working with other redirections, I do not want to use a PHP redirection. I have no access to my registrar's CNames so I cannot redirect them through there. I have placed a .htaccess file in which I have the below code in the root folder and have tried placing it in the subdomains root folder as well.
This is driving me bananas !
Thank you.
Don't forget that you can use a part of the RewriteCond expression in the rule, just like you can match a part of the RewriteRule expression:
RewriteCond %{HTTP_HOST} ^www\.(.*)
RewriteRule (.*) http://%1/$1 [R]
The %1 matches the part in braces in the RewriteCond, and the $1 matches the part in the RewriteRule. This rewrite does make the assumption that you are only using http on the standard port 80, but will basically remove any www. from the beginning of the requested URI. From that point, redirecting domain.tld to subdomain1.domain.tld should be straightforward:
RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule (.*) http://subdomain1.example.com/$1 [R]
As an aside, you probably shouldn't be using [R=301] until you have the rewrite working correctly, as the 301 means it's a permanent rewrite, but during testing it is prone to change.