Iam working on Ubuntu.(Linux)
I want to redirect 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
Related
After the vulnerability audit on my site, I found a wrong image having an extension like ABC.php.jpg
I have renamed it to ABC.jpg
Now the problem is, around 100k mails already sent and we keep the log (by saving the whole content in DB) of the mail and provide whenever required to a higher authority (By trigger mail with content which is in DB).
So I can't go in every mail and change the URL. What I want is, to access the URL (ABC.jpg) with URL ABC.php.jpg.
I have implemented some rules with htaccess file but they are simply redirecting the URL, and with this, image in mails will not load.
I guess you have the redirect flag in your mod_rewrite rules [R].
As in:
RewriteRule ^ABC.php.jpg $1/ABC.jpg [R=301,L]
Just drop that flag and it should load the content without the redirection.
RewriteRule ^ABC.php.jpg $1/ABC.jpg [L]
If you want the script to load image, you can do in your ABC.jpg.php:
<?php
echo file_get_contents("path/to/abc.jpg");
Note that you can't use the extension .jpg, if you want the server to execute your script. So it should end with .php
So in your image tag you could use this as URL:
<img src="http://host/ABC.jpg.php" />
Hope that helps
As per jeprubio's answer, I have found that we generally use this htaccess rule.
RewriteRule ^ABC.php.jpg $1/images/ABC.jpg [R=301,L]
in which R uses for Redirect and L is for to stop further processing the ruleset, So basically if we remove R rule from rule, it won't redirect to new URL.
Instead of it, it will serve response from new URL while hitting the old URL, So the final rule will be like this.
RewriteRule ^ABC.php.jpg $1/images/ABC.jpg [L]
i tryed to search everywhere for this problem but i didnt found nothing.
I want to make make a url seo friendly so i used this code:
RewriteEngine on
RewriteRule ^Homepage index.php [NC,L]
Then i want to redirect to it so i tryed to write this code:
RewriteRule ^index.php$ http://localhost/siti/socialmark/Homepage [R=301,L]
The error it's a loop of redirections, can someone help me?
SORRY FOR MY BAD ENGLISH!
The rewrite rules don't just make the URL string look different, it actually directs the user to the file at the end of the path even if you don't see it in the address bar. If Homepage is a directory containing index.php, even if that php file name doesn't appear in the URL, then it's causing a loop because it's directing you to a directory with an index.php.
The rule is executed every time that page loads. So, you're redirecting to a page which runs the redirect script, so it runs the rule to redirect again, and that causes the loop. What you want to do is create a condition that says "Don't run this code if the requested page is http://localhost/siti/socialmark/Homepage"
Something like this (you may have to adjust it)
RewriteBase /
RewriteCond %{REQUEST_URI} !=/siti/socialmark/Homepage
RewriteRule ^Homepage index.php [NC,L]
For more details, see the caveats and example here:
http://httpd.apache.org/docs/2.2/rewrite/flags.html#flag_l
Consider my domain name is
www.mydomain.com
Consider a page request
www.mydomain.com/user/register
I want to add a custom word after base URL for every request inside mydomain.com.example
www.mydomain.com/customword/
www.mydomain.com/customword/user/register
Can we do this using URL rewriting in htaccess file ?
Actually it should execute 'www.mydomain.com/user/register' internally...but externally the URL should look like www.mydomain.com/customword/user/register.
You could create the directory "register", and put an index file inside it that performs the action.
That's probably the simplest way without url rewriting anyway.
UPDATE (since the question was updated)
In .htaccess:
RewriteEngine On
RewriteRule ^([A-Za-z0-9-.]+)/user/register/?$ user/register.php?customword=$1
register.php will receive a GET request:
//User went to www.mydomain/word/user/register
echo $_GET['customword']; // will return word in this case
Make sure that you have mod_rewrite enabled :)
Yes, you can do it with htaccess
Here is an example which will add a trailing slash with url if it doesnt contain trailing slash
http://enarion.net/web/htaccess/trailing-slash/
edit formatting updated
If you are serving one site from this then the following should work:
Edit your .htaccess file to do a url rewrite
accessing www.yourdomain.com/user/registry will actually server content from www.yourdomain.com/customword/user/registry
RewriteEngine On<br>
RewriteCond %{REQUEST_URI} !^/customword/<br>
RewriteRule ^(.*)$ /customword/$1
You haven't mentioned what kind of site you;re using..eg: PHP, MVC etc as you could do similar thing in there as well.
I have made one file say a.php. Now I want some thing like if one tries to open a.php then He should ne redirected to another page of same directory of site.
I want it throght .htaccess file.
I have written this code in my .htaccess file
RewriteEngine On
# This allows you to redirect index.html to a specific subfolder
Redirect /b.php /a.php
both pages are stored in same directory..
You could try
RewriteEngine On
RewriteRule /a\.php /otherfile.php [R=301,L]
Change the filenames were necessary.
If you could be more specific as to what you wish to achieve,perhaps I can supply you with a better solution.
Question that might help:
Is it only for the file a.php, or there are other request that will be handled the same way as this pattern?
Hope it helps!
Please refere to the link below. I think so it will help you out.
How do I redirect my site using a .htaccess file?
Thanks
Or, you can use this if your script has a request string.
Like you want to stay on the same page, but not to show a request string.
(in case you want to post or get some data through scripts)
RewriteEngine on
RewriteRule ^/path/to/a/([0-9]+) /path/to/a.php?foot=$1 [PT]
Also, you should consult with the link Codemaster Gabriel and Arvind Sridharan gave you.
you can use header function in order to redirect..
try this
inside a.php
type:-
<?php
header ('location: b.php'); //lets say b.php is the file you want to redirect
?>
it will work..
and it is recommended to use this instead of .htaccess due to security reason of apache
In my .htaccess file I have defined the following rule to make my register page URL as http://example.com/register/
RewriteRule register/ /register.php
The above rule is perfectly fine but I can access my register page from http://example.com/register/ as well as from http://example.com/register.php.
I don't want that user will be able to access the URL from http://example.com/register.php URL, is there any RULE which I can define in .htaccess to stop execution of register.php URL or simply redirect any direct register.php request to /register/
If you are doing this to avoid getting multiple links to the same content, you can simply don't use "register.php" anywhere on your page. I think no search engine will "guess" for a certain file type and if there are no security concerns you are on the safe side, because in my opinion no user will link to this file either. However if you want to be certain just reroute all your functionality through an index.php via one line in your .htaccess which should be placed inside your www-root directory:
RewriteEngine on
RewriteRule ^(.*?)$ index.php?file=$1
In your index.php you can then simply choose which function/file to invoke by breaking down and checking the $_GET["file"] parameter. To make 100% certain no one can access your register.php file directly just move it (and all your others) to a separate directory and include a .htaccess file with the following line:
DENY from all
There are a couple of other options to prevent direct access. Just define() a variable somewhere in your index.php and at the top of your register.php just put
defined('access') or die('Intruder alert!');
at the top. Another way could be to be honest and simply tell search engines that your content has been moved and that they no longer should use the old link:
header("Status: 301"); /* Content moved permanently */
header("Location: http://yourserver/Register/");
exit;
Update
Just one more thing that crossed my mind, you can also check $_SERVER["REQUEST_URI"], whether the user attached any ".php" and act accordingly by either denying access completely or just redirecting to the new location.
It is true that you cannot use location directive, but you can actually paste .htaccess file into any directory.
Just if you put this into it, say:
Options -Indexes
order allow,deny
deny from all
you can copy paste this file into any (root) directory you want to protect from external execution.
To check the initial requested URL path, you need to use the request line. So try this rule:
RewriteCond %{THE_REQUEST} ^GET\ /[^?\s]+\.php[/?\s]
RewriteRule (.+)\.php$ /$1 [L,R=301]
And then again your rule (in a slightly modified way):
RewriteRule ^register/$ register.php
If you want to completely block /register.php by using mod_rewrite, use a variant of SleepyCod's answer:
RewriteCond %{REQUEST_FILENAME} register\.php [NC]
RewriteCond %{IS_SUBREQ} false
RewriteRule .* - [F,L]
Explanation:
[NC]: Makes the condition case-insensitive, just in case you're on a windows box.
Condition 1: The requested filename is 'register.php', and
Condition 2: The request is no subrequest (this is important, since every new round through RewriteRules actually creates subrequests).
Rule: essentially do nothing
Flags: [F]: Send an 403 Forbidden header, [L]: This is the last rule to apply, skip all following rewrite rules
Rewriting correctly is an art by itself. I suggest you carefully read http://httpd.apache.org/docs/2.2/rewrite/.
Cheers,
Try this.
RewriteCond %{REQUEST_FILENAME} ^register\.php$ [NC]
RewriteRule ^/register register.php
Or this
Redirect register.php /register
Ignoring the user-experience part, you can implement the new rel=canonical link to sort out the search engines.
Although, for this case you should probably just use a 301 redirect from /register.php to /register/
In register.php
if ( stristr( $_SERVER['REQUEST_URI'], '.php' ) )
{
header ('HTTP/1.1 301 Moved Permanently');
header ('Location: /register');
}