Redirection using .Htaccess file
Iam working on Ubuntu.(Linux)
I want to red开发者_StackOverflowirect from the page one.php to two.php, which are in a folder 'test'
How can i do this, using .htaccess file?
Any other setting is needed for this?
Or .htaccess redirection will not work for local system
It should work fine. Assuming the URL is http://localhost/test/one.php:
RewriteEngine On
RewriteBase /test/
RewriteRule ^one\.php$ two.php [L]
That'll do an invisible redirection—the browser won't know, and won't show it in the URL. If you want to do a different kind of redirect, you can specify R=###
in the flags, where ###
is the HTTP status code. For example, to perform a permanent redirect:
RewriteRule ^one\.php$ two.php [R=301,L]
the question title says "using .Htaccess", but in the last line of the question you said .htacess are not working.
so here is a solution without using .htaccess at all.
you could add a simple function in one.php file to send headers to the client (the web browser for example) to resend the request to the second address, say: two.php.
open file one.php and add this line of code in it:
<?php
header('Location: two.php');
?>
make sure you have print/echo anything (even a simple space character) before this code. because this line sends the HTTP header to the client, but if you output anything before this line, it would have gone in the body so headers will be closed.
In your test
folder, you can put a .htaccess
file, with the RewriteRule
to do that redirection :
RewriteEngine On
RewriteRule one.php two.php
With this, the page executed on the server would be two.php ; but the URL in the browser would still be one.php
Depending on whether you want this redirection to be seen from the user, you'll to set, or not, the redirection code, and use, or not, the [R]
flag.
For a permanent redirection, that would appear in the browser (i.e. the URL would really become two.php in the browser's address bar), you'd use :
RewriteEngine On
RewriteRule one.php two.php [R=301]
For more informations, you can take a look at the URL Rewriting Guide
精彩评论