How to use htaccess? - php

<IfModule mod_rewrite.c>
RewriteEngine On
</IfModule>
this is on my server (mypage.com) in .htaccess file.
How can i make:
If i open here.mypage.com then this redirect me to mypage.com/here.html
If i open mypage.com/now then this redirect me to mypage.com/now/test.html
My page is in PHP.

1.If i open here.mypage.com then this redirect me to mypage.com/here.html
As long as here.mypage.com is the same document root the htaccess file is in, add:
RewriteCond %{HTTP_HOST} ^here\.mypage\.com$ [NC]
RewriteRule ^$ http://mypage.com/here.html [R=301,L]
2.If i open mypage.com/now then this redirect me to mypage.com/now/test.html
Add:
RewriteCond %{HTTP_HOST} ^mypage\.com$ [NC]
RewriteRule ^now$ /now/test.html [L]

RewriteEngine on
RewriteRule ^now$ now/test.html
That's for the second one. The first one, you'll have to use your website cPanel (or similar) make a new subdomain, and point the subdomain to the main public_html folder, but we'll likely need more information about your server to give exact help.
EDIT:
RewriteCond %{HTTP_HOST} ^here.mypage.com [NC]
RewriteRule ^/here.html [L]
That should do the subdomain.

to redirect here.mypage.com to mypage.com/here.html, this will require more than to add a rule to the .htaccess (see => Setting a subdomain).
For the other one you can add :
RewriteRule ^now$ now/test.html

Give this a try:
Redirect 301 here.mypage.com mypage.com/here.html
Redirect 301 mypage.com/now mypage.com/now/test.html

Related

Redirect always to index.php

I'm trying to relocate a site into a temporary folder in order to install a CMS in the root. The redirect works but all the internal links in the site now take me back to the index page in the temporary folder. The .htaccess in the root is as follows:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/old/ [R=301,NC]
it's because you're redirecting everything to the new url. You should put something like this:
RewriteRule ^$ http://www.example.com/old [R=301,NC]
RewriteRule ^(.*)$ http://www.example.com/old/$1 [R=301,NC]
PS: The $1 take the arguments in expression
PS2: You should not use code 301 for a temporary redirect, the 302 is more appropriate https://en.wikipedia.org/wiki/HTTP_302

How to redirect homepage to subfolder without changing URL (still keep www.example.com)

My current .htaccess is in httpdocs.
How can I configure it to help domain point to subfolder is /vn/
and still keep: www.example.com instead of www.example.com/vn/
Try the following near the top of your .htaccess file in the document root:
RewriteEngine On
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule (.*) /vn/$1 [L]
This internally rewrites all requests that hit the document root to the /vn/ subdirectory. The RewriteCond directive that checks against the REDIRECT_STATUS environment variable ensures the request is only rewritten once - thus avoiding a rewrite loop.
In .htaccess add these lines :
RewriteEngine On
RewriteRule !^blog/ /blog%{REQUEST_URI} [L,R=301]
Or in some hosts :
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?YourDomainName.com$
RewriteRule ^(/)?$ blog [L]

How can i redirect all subfolders to a single root folder

I am trying to redirect all calls to mysite.com/api/foo/bar/json?param1=1... to mysite.com/api
I've tried
RewriteEngine On
RewriteRule "^api/?(.*)" "/api" [R=302]
But it doesn't work because it redirects in a loop
You need to exclude the destination path you are redirecting to
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/api/?$
RewriteRule ^api/?(.*) /api [R=302]
First check file permission maybe you don't have to made edit on the .htaccess file. Then insert this line of code.
RewriteEngine on
RewriteRule ^(.*)$ http://www.example.com/$1 [R=permanent,L]

Redirect all folders to subdomains htaccess

I am trying to redirect every folder from domain.com to it's subdomain.
Examples:
domain.com/a -> a.domain.com
domain.com/b -> b.domain.com
domain.com/about -> about.domain.com
So basically, every folder redirected to it's subdomain. For the first example, folder a contains index.html. When I browser to domain.com/a it should redirect the url to a.domain.com but use the index.html file from the a folder.
My current htaccess looks like this:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteCond %{HTTP_HOST} !^www\.domain\.com$
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com$
RewriteCond %{REQUEST_URI} !^/%1/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /%1/$1
RewriteRule ^(/)?$ %1/index.html [L]
What works
For now, if I navigate to test.domain.com, it's working, it's taking the index.html file from the test folder. If I go to domain.com/test it's still working, but I want to disable this. I want to redirect domain.com/test to test.domain.com as well, but still use the index.html files from the test folder.
Another thing that is in the htacces file is to redirect www to non-www.
Any suggestions would be appreciated.
EDIT:
Forgot to mention that I've tried to add this line:
RedirectMatch 301 ^/test/(.*)$ http://test.domain.com/$1
It's doing the redirection from domain.com/test to test.domain.com/index.html, but it's not loading the page right, I get an error (Page isn't redirecting properly).
keep like
RedirectMatch 301 ^{RELATIVE PATH}$ {ABSOLUTE_PATH}

Htacess Rule for redirect to a URL based on HOST Name

I have configured my project in the server
Example http://www.example.com
In that there is one page http://www.example.com/products/index
Now I have other new domain http://dev.ny-sample.com need to point
I pointed the new domain to http://www.example.com/products/index
But Now I need to write a rule in my HTACESS File of http://www.example.com/
If the user type these url http://dev.ny-sample.com then its should redirect to http://dev-nysample.com/products/index
put this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?nysample\.com$ [NC]
RewriteRule ^/?$ http://%{HTTP_HOST}/products/index [R=301,L]
Try it, It is working fine for me.
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/$
RewriteRule (.*) http://www.example.com/product/index [R=301,L]
<IfModule mod_rewrite.c >
RewriteEngine On
Redirect / http://dev-nysample.com/products/index
<IfModule>
Eventually I made this way in my index.php
if($_SERVER['HTTP_HOST'] =='dev.ny-sample.com' && ($_SERVER['REQUEST_URI'] == "/"))
{
header('Location: http://dev.ny-sample.com/products/index/');
}
I tried all the htaccess way it didn't worked for me, May be I don't know the right way.

Categories