This is similar to question mod_rewrite: Being redirected to root folder; I want it to stay in sub-folder
but the answer there is not working for me.
I have a main site which redirects via an index.php handler, but I want a pre-live test area which is held in a subdirectory /UA/
I need requests for UA/ to pass through and be handled in the sub directory, I have tried both RewriteRule ^/UA - [L] and RewriteCond %{REQUEST_URI} !^/UA in my .htaccess of the main directory but this hasn't worked. I've tried with/without /'s
Here are my two .htaccess files:
RewriteEngine on
ReWriteBase /
# Redirect to HTTPS site
RewriteCond %{HTTP_HOST} ^my\-site\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.my\-site\.com$
RewriteRule ^/?$ "https\:\/\/www\.mysite\.com\/" [R=301,L]
RewriteCond %{HTTP_HOST} ^mysite\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.mysite\.com$
RewriteRule ^/?$ "https\:\/\/www\.mysite\.com\/" [R=301,L]
#Allow UA access
RewriteRule ^/UA - [L]
#Allow certain file types to access directly
RewriteRule \.(css|js|png|jpg|gif|woff|eot|ttf|svg|ico)$ - [L]
# Catch all
RewriteCond %{REQUEST_URI} !^/UA
RewriteRule .* index.php [L]
RewriteEngine on
ReWriteBase /UA/
#Allow certain file types to access directly
RewriteRule \.(css|js|png|jpg|gif|woff|eot|ttf|svg|ico)$ - [L]
# Catch all
RewriteRule .* index.php [L]
The problem is that the site continues to direct to the Live index.php version not the one in /UA/ with a URI of https://www.mysite/UA/
Add this line to always have directory slash on in your root .htaccess:
DirectorySlash On
Keep /UA/.htaccess like this:
RewriteEngine on
ReWriteBase /UA/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !\.(css|js|png|jpg|gif|woff|eot|ttf|svg|ico)$ [NC]
RewriteRule ^ index.php [L]
Related
My main domain is tied to my public_html folder but I use an .htaccess redirect to change the root directory to a sub-directory in the folder to make it easier to host multiple sites. Example:
public_html
-- .htaccess
-- site1
---- .htaccess
-- site2
-- site3
The .htaccess file in the root directory is as follows:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteCond %{REQUEST_URI} !^/subdirectory/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /subdirectory/$1
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteRule ^(/)?$ subdirectory/ [L]
This works fine, but I think there are issues with this and the .htaccess file in my site1 directory. If I enter the URL it sometimes redirects to example.com/site1. So the website appears exactly the same on example.com and example.com/site1 but I don't want it to appear as example.com/site1. The second .htaccess file inside the site1 directory is show below.
RewriteEngine on
# Change default directory page
DirectoryIndex /home/
# Remove .php extension
RewriteCond %{THE_REQUEST} ^GET\ /[^?\s]+\.php
RewriteRule (.*)\.php$ /$1/ [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/(.+)/$
RewriteCond %{DOCUMENT_ROOT}/%1.php -f
RewriteRule ^(.*)/$ $1.php [L]
# Ensure all directory URLs have a trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !\/$
RewriteCond %{REQUEST_URI} !\/[^\/]*\.[^\/]+$
RewriteRule .* http://%{HTTP_HOST}%{REQUEST_URI}/ [L,R=301]
# Same for HTTPS
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !\/$
RewriteCond %{REQUEST_URI} !\/[^\/]*\.[^\/]+$
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI}/ [L,R=301]
# Redirect default error page to home page
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^(/|error/)?$ /home/ [R=301,L]
# Error Documents
ErrorDocument 400 /error/?e=400
ErrorDocument 401 /error/?e=401
ErrorDocument 403 /error/?e=403
ErrorDocument 404 /error/?e=404
ErrorDocument 500 /error/?e=500
# Prevent viewing of htaccess file
<Files .htaccess>
order allow,deny
deny from all
</Files>
I'm not sure if it's an issue with the various rewrite rules I'm using that affects the URL, but I would appreciate any help if it can be resolved.
Thanks in advance and I apologise if my description isn't great.
Try
# Ensure all directory URLs have a trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !\/$
RewriteCond %{REQUEST_URI} !\/[^\/]*\.[^\/]+$
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1/ [QSA,L,R=301]
# Same for HTTPS
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !\/$
RewriteCond %{REQUEST_URI} !\/[^\/]*\.[^\/]+$
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [QSA,L,R=301]
It works for me. %{REQUEST_URI} contains site1, so don't use it.
QSA adds any URL parameters like ?param1=val1¶m2=val2 If you don't use them, you may omit QSA.
Also while testing replace R=301 to R=302 (temporary redirect), else your browser remembers the rule and doesn't send request again.
I have a few domain names, and recently updated my website to be run from a single file (index.php). Request URI is the same other than no .php . My host is GoDaddy linux unlimited (yeah...).
I require my .htaccess to redirect/rewrite the following
Redirect domain1.com to www.primary.com
Redirect www.domain1.com to www.primary.com
Redirect domain2.com to www.primary.com
Redirect www.domain2.com to www.primary.com
Force https://
Force www.
Remove .php from any requests (/marketing/cost.php to /marketing/cost)
Remove trailing slashes
Im currently using the following code to pass all request to index.php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
The index.php file handles all request. Based on ($_SERVER['REQUEST_URI']) index.php loads in content from database.
I know trailing slashes are recommended, but would prefer to keep off; i think domain.com/sec/nom?x=23 looks better than domain.com/sec/nom/?x=23
I have attempt a few variations in .htaccess my self, and work locally, but not on GoDaddy.
Have it like this:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.primary\.com$ [NC,OR]
RewriteCond %{HTTPS} off
RewriteRule ^ https://www.primary.com%{REQUEST_URI} [L,NE,R=302]
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=302,L]
## Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)/$ /$1 [NE,R=302,L]
RewriteRule ^index\.php$ - [L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
I am using an AWS EC2 instance to host a website. I have an installation under /var/www/html/xyz/ dir & thus if I have to access website homepage, I have to specify link as www.example.come/xyz. I want that www.example.com should be redirected to www.example.com/xyz
Any idea how can I redirect www.example.com to www.example.com/xyz automatically (httpd config or anything similar)?
I checked online but most of the posts are related to subdomain redirects e.g. redirect admin.example.com to www.example.com which is not my issue.
Thanks!
my .htaccess file is:
<IfModule mod_rewrite.c>
RewriteEngine on
# Some hostings require RewriteBase to be uncommented
# Example:
# Your url is http://www.yourcompany.com/xyz
# So "RewriteBase" should be:
# RewriteBase /xyz
Options -MultiViews
RewriteBase /xyz
RewriteCond %{REQUEST_URI} ^api/(.*)$ [or]
RewriteCond %{REQUEST_URI} .*/api/(.*)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .*api/(.*)$ api.php?_d=$1 [L,QSA]
RewriteCond %{REQUEST_URI} \.(png|gif|ico|swf|jpe?g|js|css|ttf|svg|eot|woff|yml|xml)$ [or]
RewriteCond %{REQUEST_URI} store_closed.html$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*?)\/(.*)$ $2 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L,QSA]
</IfModule>
Another option is to change the document root of your site. If you are running Ubuntu, you should have a file in /etc/apache2/sites-available/YOUR_SITE.conf (maybe default.conf). Just edit it and change the DocumentRoot to point to your subfolder.
Create new .htaccess file with following code
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/$
RewriteRule (.*) /xyz/ [R=301]
you should have mod_rewrite enabled.
Use this code in your htaccess file, this will redirect the user to the subfolder without changing th URL www.domain.com
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?domain2\.com$ [NC]
RewriteRule !^subdir/ /subdir%{REQUEST_URI} [L,NC]
Another method with URL www.domain.com/subdir
RewriteEngine On
RewriteRule ^$ /subdir[L]
You can use the following code in Root/.htaccess:
RedirectMatch ^/$ /xyz
This will redirect
http://example.com
to
http://example.com/xyz
And you want to Rewrite http://example.com/foo to http://example.com/xyz/foo , you can use mod_rewrite :
RewriteEngine on
RewriteRule ^/?((?!xyz).*)$ /xyz/$1 [L]
Hope this will help !
RewriteEngine on
RewriteCond %{HTTP_HOST} domain\.com [NC]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^(.*)$ /subfolder/$1 [L]
or this
Redirect 301 /path/to-old-url http://www.cyourdomain.com/path/to-Try this new-url
I want to redirect all pages on my site (including index) to UnderWork.html
and I'm doing this using .htaccess with this code:
RewriteEngine on
RewriteRule ^(.*)$ UnderWork.html
...and its works fine.
Now I am adding some more code to my .htaccess to redirect all traffic to the non-www domain and now my code looks like this:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.domain\.in
RewriteRule ^(.*)$ http://domain.in$1 [R=permanent,L]
RewriteRule ^(.*)$ UnderWork.html
Here is where I have a problem. If I enter a URL like: domain.in/xyz then it works as before, but if I use a URL like: www.domain.in/xyz then Apache converts it to coincart.inxyz/.
What am I doing wrong here? How can I get what i want? Thanks in advance.
Below are rules that work for me:
RewriteEngine On
# Adding trailing slash for directory requests.
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_URI} !^/www$
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.+[^/])$ http://example.com/$1/ [R=permanent]
# External redirection from www subdomain to non-www domain.
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^/?(.*) http://example.com/$1 [L,R=permanent]
# Internal redirection to index.php for nonexistent URLs.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !\.(jpg|jpeg|png|gif)$
RewriteRule . /index.php [L,QSA]
To show a custom page when a directory root (domain root in particular) is requested, use the Apache's DirectoryIndex directive:
DirectoryIndex UnderWork.html
Here is my site url structure as follows: http://www.sitename.com/new/about-us .
What I really want to do now is hide the directory name 'new' from the above url, but the admin url should remain unchanged.
The admin url would be like : http://www.sitename.com/new/admin .
My previous .htaccess code as follows:
RewriteEngine On
RewriteBase /new/
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/new/$1 [R=301,L]
RewriteRule blog/ - [L]
RewriteRule (^wlp) - [L]
RewriteRule admin/ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %(REQUEST_FILENAME} !-d
RewriteRule ^([\S\s/+.]+)/?$ index.php?url=$1 [QSA,L]
Here is my server directory structure:
/public
/new
.htaccess
index.php
about-us.php
/blog
/admin
Any help in this regard will be highly appreciated.
It sounds like what you are trying to accomplish is a common technique which I've seen before for forwarding all public_html requests to public_html/public to essentially hide the contents of the public_html directory from the user and making public_html/public the new web root.
Try using this in your public_html/.htaccess file (you can write further htaccess in public_html/new/.htaccess):
RewriteEngine on
RewriteBase /
#enforce www subdomain
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} ^sitename.com [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
#forward all requests, except new/admin, to the 'new' directory without the user's awareness
RewriteRule new/admin - [S=2]
RewriteRule ^$ new/ [L]
RewriteRule (.*) new/$1 [L]
This .htaccess in / should do:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^new/
RewriteRule ^(.*)$ new/$1 [L]