Redirect url to another - php

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

Related

.htaccess redirect sub-directory to root issue

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&param2=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.

how to redirect all page request to one page without url change

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

.htaccess returning mod_rewrite redirect to the index page of the www folder of wamp

I am trying to create "pretty urls for dynamically created pages. My desire is to render all pages from the index page.
I am working on my computer using wamp 2.5/apache 2.4.9
The apache httpd.conf is set to:
Listen 0.0.0.0:7080
Listen [::0]:7080
DocumentRoot "c:/wamp/www/"
ServerName localhost:7080
I have tried several different approaches which have resulted in everything from a blank page returning to a 404 error page. With the code below it returns to the index.php in the /www/ folder in wamp.
This is my non-working .htaccess code:
# Turn Rewrite Engine
Options +FollowSymLinks
RewriteEngine on
#RewriteCond %{REQUEST_FILENAME} !-d
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-l
# Pages
RewriteRule ^([a-zA-Z0-9]+)$ index.php?topic=$1
# Rewrite to www.
RewriteCond %{HTTP_HOST} ^localhost:7080/demo [nc]
RewriteRule ^(.*)$ http://www.localhost:7080/demo/$1 [r=301,nc]
The clupret line of code is this I believe:
RewriteRule ^([a-zA-Z0-9]+)$ index.php?topic=$1
which I am thinking is some sort of path issue but am not sure... any assistance would be appreciated.
The URL in the address is: http://localhost:7080/demo/test where demo is the site folder in the wamp/www/
http://localhost:7080/demo/test/ (with the forward slash) returns a 404 error.
Your htaccess is lacking a rewrite base.
Just after: RewriteEngine on
Add RewriteBase /demo/
It is strictly not allowing trailing slashes
Replace this line: RewriteRule ^([a-zA-Z0-9]+)$ index.php?topic=$1
withthis code:
RewriteCond %{REQUEST_URI} !-f
RewriteRule ^([a-zA-Z0-9]+)$ index.php?username=$1 [N,L]
This will allow urls ending trailing slashes to force back to your redirect url.
Remove this from the last line:
http://www.localhost:7080.
Leave it as just: /demo/$1 [r=301,nc]
The second to the last line is not doing a lot. You can do and I will advice you do away with it.
Please do remember that when leaving the localhost environment for an online host, replace all incidence of "/demo/" with just "/"
Your final htaccess should be someting like this:
# Turn Rewrite Engine
Options +FollowSymLinks
RewriteEngine on
RewriteBase /demo/
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-l
#RewriteCond %{REQUEST_FILENAME} !-d
# Pages
#RewriteCond %{REQUEST_URI} !-f
RewriteRule ^([a-zA-Z0-9]+)$ index.php?username=$1 [N,L]
# Rewrite to www.
RewriteRule ^(.*)$ /demo/$1 [r=301,nc]
I hope this helps
Try these rules in /demo/.htaccess:
Options +FollowSymLinks
RewriteEngine on
RewriteBase /demo/
# Rewrite to www.
#RewriteCond %{HTTP_HOST} ^localhost$ [NC]
#RewriteRule ^ http://www.%{HTTP_HOST}:7080%{REQUEST_URI} [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([a-zA-Z0-9]+)/?$ index.php?topic=$1 [L,QSA]

.htaccess pass request on for /UA/ folder

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]

htaccess add trailing slash and force www with clean urls

I'm using my htaccess file with mod_rewrite to create clean urls like this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
</IfModule>
I would also like to force the site to have the 'www' subdomain and most importunately add a trailing slash if the url doesn't have one.
I am an absolute noob with mod_rewrite and I've tried accomplishing this on my own by combining other code I found on google (sad I know), but I always end up with a 500 error.
Here's the code I found for force www:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteCond %{HTTP_HOST} ^www\.domain\.tld$ [NC]
RewriteRule ^(.*)$ http://domain.tld/$1 [R=301,L]
</IfModule>
Thanks for your help.
Try separating out the www and the trailing slash check. This is tested and hopefully working for you. You didn't say if you're running placing at domain root or in a subdirectory - usually good info when asking for help with htaccess.
RewriteEngine On
# Assuming you're running at domain root. Change to working directory if needed.
RewriteBase /
#
# www check
# If you're running in a subdirectory, then you'll need to add that in
# to the redirected url (http://www.mydomain.com/subdirectory/$1
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.mydomain.com/$1 [R=301,L]
#
# Trailing slash check
# Don't fix direct file links
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ $1/ [L,R=301]
#
# Finally, forward everything to your front-controller
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php [QSA,L]
To debug, comment out the individual sections and see what is/isn't working.
Use this and forgot your problems ;)
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule ^(.*?)/*$ http://%1/$1/ [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://your-domain.ru/$1/ [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>

Categories