Redirecting to another page - htaccess - .htaccess

I'd like to redirect the user to a page named test.php when he types the url of my site (example www.domain.com).
Pratically by typing www.domain.com the browser should show www.domain.com/test.php

At index.html or index.php, you could add meta refresh tag like this :
<meta http-equiv="refresh" content="5;www.domain.com/test.php" />
If you use index.php, the syntax would be like this:
echo '<meta http-equiv="refresh" content="5;www.domain.com/test.php" />';
Add http:// at the url if You want to redirect to another domain.

In top of your .htaccess file try this rules (except files and directories rewrite anything www/non www to example.com/test.php)
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com [NC]
RewriteRule (.*) http://example.com/test.php [L]
</IfModule>

Related

Redirect to homepage if file or directory doesn't exist using .htaccess

I am Hosting a domain on free hosting site 000webhost, I want to redirect all files or folder that doesn't exist to my homepage. The Code, I have used is:
RewriteEngine on
rewritecond %{http_host} ^domain\.com$ [nc]
rewriterule ^(.*)$ http://www.domain.com/$1 [r=301,l]
RewriteCond %{THE_REQUEST} ^.*/index.html
RewriteRule ^(.*)index.html$ http://www.domain.com/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.html
It works, but the url doesn't change to homepage. Like if I am trying to open www.domain.com/blaa it redirects me to home page but the URL is still www.domain.com/blaa. I want it to be changed to www.domain.com.
If I am trying to open a valid directory, which exist in my root folder, it also redirects to homepage. Like if I am trying to open www.domain.com/folder, it should result in files and directories in that folder, instead of that it redirects me to homepage and web address is still same like www.domain.com/folder.
Also I want to keep using the above 2 rules to open website with WWW and index.html with www.domain.com
Please help me.
Thanks in advance.
As per your questions's title, to redirect to homepage if the request is not for a file or dir, you can use :
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ / [L]
You can also use ErrorDocument directive to rewrite non-existent requests to homepage
ErrorDocument 404 /

How to render same page with different url using htaccess

I have new domain and for now I want to render all url to home page.
eg.
abc.com/abc
abc.com/abc/xyz
abc.com/abc/xyz/123
abc.com/abc/abc/xyz/123/so_on
also url will show only current home page ( index.html (or .php) ) instead of
"Not Found, The requested URL /index.php was not found on this server."
How can I do that using htaccess?
You can use a rewrite rule in .htaccess
RewriteEngine On
RewriteRule ^.*$ index.php [NC,L]
This will send all requests to index.php file(or you can change it to index.htm etc.)
also you can bypass static files like css/js and etc:
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

apache .htaccess redirect - clean url

I am working on a php redirect script which 302 redirects visitors to other sites when they access a redirect url..
The script gets a variable (id) from the url and then redirects the visitor to the specific page.
The url structure is : example.com/redirect/index.php?id=test
At the moment all redirects work if I use "ugly" urls, but I want to strip all unnessecary information out of the url with .htaccess rewrites for better usability.
Which .htaccess rewrite rules do I need to make the above shown urls look like : example.com/redirect/test
I am currently using the following .htaccess rules
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^index\.php$ - [L]
RewriteRule (.*) ./index.php?id=$1 [L]
</IfModule>
but they only work for urls like example.com/redirect/index.php?id=test if I try example.com/redirect/test I get a 404 error page.
It might be good to know, that I have 2 .htaccess files, one in my root directory and one in the root/redirects/ directory.
Best regards !
Try this:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/([^/]+)?/?([^/]+)?/?([^/]+)?/? [NC]

How to remove page extension (i.e. /profile.html) with htaccess

How to remove page extension (i.e. profile.html) with htaccess ? So if I load www.mysite.com/profile.html I need profile.html to be remove it.
this removes only index.html , if I change it to profile.html it just goes to mysite.com and loads home page.
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{THE_REQUEST} \/index.html\ HTTP [NC]
RewriteRule (.*)index.html$ /$1 [R=301,L]

Add A 301 Redirect to Codeigniter .htaccess

I've been trying to set up a redirect for a page that recently moved. The page was originally at http://example.com/foo/, but has since moved to http://example.com/foo/bar/ .
I tried the following rule in my sites .htaccess file:
RedirectMatch 301 ^/foo/$ /foo/bar/
However going to the url http://example.com/foo/ resulted in a redirect to the url http://example.com/foo/bar/?/foo/. While the url works and the page I want to redirect to loads, I would quite like to get rid of the extra ?/foo/ at the end of the url.
Here is my full .htaccess:
RewriteEngine On
RewriteBase /
# add trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ $1/ [R=301,L]
# allow access to certain directories in webroot
RewriteCond $1 !^(index\.php|robots\.txt|css/|lib/|js/|images/|^(.*)/images)
# gets rid of index.php
RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# page redirects
RedirectMatch 301 ^/foo/$ /foo/bar/
Adding a RewriteRrule to the top of the .htaccess after the RewriteBase / file solved the problem.
RewriteRule ^foo/$ /foo/bar [R=301,L]
I found it easier to redirect from controller instead of .htaccess because .htaccess was adding a querystring at the end.
For example I've put this in my controller's action:
if ($this->uri->segment(2)==='old_url') {
redirect(base_url() . $this->lang->lang() .'/new-url', 'location', 301);
}

Resources