Forum Moderators: phranque

Message Too Old, No Replies

.htaccess Redirect not working

redirect from .html to .php in another folder

         

aarti_k

8:00 am on Oct 21, 2010 (gmt 0)

10+ Year Member



I am stuck with this one. I want to redirect from www.babamandir.org/index.html to www.web.babamandir.org/index.php which is a wordpress page and my homepage. The index.html is just a page used to display a flyer which I want to omit now. I am using below in the .htaccess file,
Redirect /babamandir.org [babamandir.org...]
But it does not seem to work. Am I missing something? Also the .htaccess file in the web folder is like this:
# Use PHP5 as default
AddHandler application/x-httpd-php5 .php

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

Any help is appreciated.

jdMorgan

2:00 pm on Oct 21, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The redirect directive will redirect the specified URL-path (ont the left) to the given full URL (on the right), plus any remaining unmatched part of the requested URL not given on the left.

Therefore,
 Redirect /a http://example.com /b [/b]
redirects a request for example.com/a to example.com/b, but it also redirects example.com/a/x to example.com/b/x -- Any requested URL-path that starts with "/a" will be redirected.

In addition, using two different modules as you have done here (mod_alias and mod_rewrite) means that you cannot control the order of execution of their directives. Apache .htaccess files are not executed as linear/sequential programs. Rather, each Apache module in turn scans the .htaccess file(s), executing directives that it recognizes. The order in which modules scan the .htaccess file(s) is set by the server itself. Therefore, if you wish to guarantee that your redirect is executed before your WP rewrite, then use mod_rewrite for both functions.

Finally, note the term "URL-path" that I used in the description of the Redirect directive above: The URL path for example.com/a is just "/a", and this is what should appear on the left side of a Redirect Directive. The hostname "example.com" is not present in the URL-paths examined by Redirect or RewriteRule directives.

As another comment, you can significantly speed up the WP rewrite code with a few simple changes to prevent unnecessary disk checks. On many sites, this will lead to noticeable increases in the page-loading speed.

So, with all that in mind,
[code]
# Use PHP5 as default
AddHandler application/x-httpd-php5 .php
#
RewriteEngine On
RewriteBase /
#
# Redirect /index.html requests to /other-folder/index.php
RewriteRule ^index\.html$ http://babamandir.org/other-folder/ [R=301,L]
#
# BEGIN modified WordPress
RewriteCond %{REQUEST_URI} !(\.(gif|jpe?g|png|ico|css|js)|^/index\.php)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# END WordPress

Note that I removed the unnecessary <IfModule> container. If included, it will cause this code to fail silently on servers which do not have mod_rewrite installed, giving you no indication of the problem. This is almost never what you would want to happen.

Jim

aarti_k

10:33 pm on Oct 21, 2010 (gmt 0)

10+ Year Member



Thank you for the prompt reply and all the explanation. I think I understand the htaccess file little more now. But using the code you mentioned I am still having some trouble with getting what I want. The site still visits the index.html and not the index.php file. I am not sure if I explained what I want very clearly before but here it is what I have:
The site url is www.babamandir.org but the site is present in www.web.babamandir.org.
There is an index.html in www folder which is used as a flyer for some events. But the main home page is the wordpress site located in www/web folder as index.php.
Now I want to skip the flyer which is www/index.html and redirect any links like www.babamandir.org or babamandir.org to www.web.babamandir.org directly. Which is still not happening.

The code you mentioned goes in the .htaccess file in the www folder right?
Also can I append non www to www redirect code in the middle like this?


# Use PHP5 as default
AddHandler application/x-httpd-php5 .php
#
RewriteEngine On
RewriteBase /
#
[b]#
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.babamandir\.org$ [NC]
RewriteRule ^(.*)$ http://www.babamandir.org/$1 [L,R=301]
#[/b]
# Redirect /index.html requests to /web/index.php
RewriteRule ^index\.html$ http://babamandir.org/web/ [R=301,L]
#
# BEGIN modified WordPress
RewriteCond %{REQUEST_URI} !(\.(gif|jpe?g|png|ico|css|js)|^/index\.php)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# END WordPress

g1smd

10:42 pm on Oct 21, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The root of your site should be accessed at the www.example.com/ URL.

You should not have to refer to the index file by name in any links. Is there a DirectoryIndex directive that also needs to be altered here?

Additionally, it is also a good idea to redirect requests for /index.php and/or /index.htm(l) to www.example.com/ too.

Finally, rule order is important. First, list the index redirect. This redirect will also fix the domain name for those requests.

Next list the non-www to www and non-canonical to www redirect. This will fix remaining non-canonical requests.

Finally, list the rewrite. It is this that will serve the content when the correct, canonical, URL has been requested.

jdMorgan

11:50 pm on Oct 21, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I believe I understand your goal, but this description is very confusing. Please clearly specify which of these are URLs (start with "http://") and which are filepaths. First, tell us your root directory filepath, and then specify all remaining filepaths as relative from that root.

For example:
The site url is www.babamandir.org but the site is present in www.web.babamandir.org

Are you saying that the path from DocumentRoot to your stored site files is "/www.web.babamandir/org/"?

There is an index.html in www folder which is used as a flyer for some events. But the main home page is the wordpress site located in www/web folder as index.php.

Or is the path from DocumentRoot to your stored site files "/www/"?

Or is "/www/" itself your DocumentRoot?

The "correctness" of the code depends on many things, including the location of the file containing that code...

Jim

aarti_k

9:52 pm on Oct 22, 2010 (gmt 0)

10+ Year Member



The site has a old url which is http://www.babamandir.org. The new url is http://web.babamandir.org (which I think means the same as http://www.web.babamandir.org). Many times the site is accessed from outside using the old url. /www/ has a index.html file. I want to directly load WP site which is in /www/web/ folder on accessing either urls. There is no problem in accessing http://web.babamandir.org because its htaccess file(/www/web/.htaccess) has the needed wp initialization. The problem is with htaccess in www folder i.e. /www/.htaccess.

The filepath is www/index.html and www/web/index.php. Wordpress(Site) files are in www/web/ folder. So /www/ is not the DocumentRoot.

Does that provide enough information? Where am I going worng. I am little new to Apache and php.

[edited by: jdMorgan at 11:41 pm (utc) on Oct 22, 2010]
[edit reason] De-link hostnames for readability. [/edit]

jdMorgan

11:43 pm on Oct 22, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> I want to directly load WP site which is in /www/web/ folder on accessing either urls.

So to be clear, you want these two identical Web sites to compete with each other for search ranking, each reducing the ranking of the other?

Jim

aarti_k

7:16 pm on Oct 23, 2010 (gmt 0)

10+ Year Member



I am sorry, I think I am not being very clear in this. I dont know how imp is search ranking here because there is only one site.

www.babamandir.org opens an index.html page. this index page then has a link to proceed to the main site, web.babamandir.org.

I know both the links show up on search but I want to redirect the www.babamandir.org/index.html to web.babamandir.org/index.php.

jdMorgan

8:46 pm on Oct 23, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



OK, I wanted to be clear, because you said, "I want to directly load WP site which is in /www/web/ folder on accessing either urls." This implied that you wanted to do an internal rewrite or a proxy through-put from the URL www.babamandir.org/index.html directly to the same content as would be served if web.babamandir.org/index.php were requested without doing a redirect to change the browser's address bar.

This creates duplicate content -- Two URLs returning identical content, and is essentially the same as competing with yourself for search engine links and ranking.

To avoid that problem you need to use external redirects to tell the client that the URL that it used was wrong.

In the directory where the file for www.babamandir.org/index.html is stored (or where it used to be stored), put a .htaccess file with:

Options +FollowSymLinks -Indexes -MultiViews
RewriteEngine on
#
RewriteRule ^(index\.html)?$ http://web.babamandir.org/ [R=301,L]

In the file directory where the script for web.babamandir.org/index.php is stored, add something like this as the final two external redirect RewriteRules (the last RewriteRules with [R=30x] on them).

# Externally redirect all direct client requests for URL-path "/index.php" to "/"
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /index\.php([?#][^\ ]*)?\ HTTP/
RewriteRule ^index\.php$ http://web.babamandir.org/ [R=301,L]
#
# Externally redirect requests for all non-blank, non-canonical hostnames to canonical hostname
RewriteCond %{HTTP_HOST} !^(web\.babamandir\.org)?$
RewriteRule ^(.*)$ http://web.babamandir.org/$1 [R=301,L]

Jim