Forum Moderators: phranque

Message Too Old, No Replies

Preventing .html from being added to URLs on redirect

Moving a page from subdomain to subdirectory, don't want .html on URLs

         

Esoos

3:50 pm on Aug 25, 2010 (gmt 0)

10+ Year Member



We're currently in the process of redirecting a WordPress blog from our site's subdomain to a one of our subdirectories. A typical page is going from:

http://blog.example.com/page.html


to:

http://www.example.com/blog/page


In the subdirectory's .htaccess file, I'm using a standard 301 redirect for each page. For example:

Redirect 301 /page.html http://www.example.com/blog/page


However, this is redirecting URLs to the version of the page with the .html appended. In the example above, the redirect ends up going to:

http://www.example.com/blog/page.html


...which is a 404 (since the real page is at:
http://www.example.com/blog/page
)

I've looked at the headers, and there's no additional redirect from "page" to "page.html" happening. Instead, redirects from the subdomain are going directly to "page.html".

Any idea what is going on? Do I need an additional redirect to strip the .html from the URL, as discussed in this thread [webmasterworld.com]. I'd like to avoid an extra redirect if possible.

One other note: The subdomain we're redirecting from is on a Linux server, and the subdirectory is on a Windows server, but that doesn't seem like it should be causing this problem.

jdMorgan

5:59 pm on Aug 25, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What you describe isn't really possible (See the Apache mod_alias "Redirect" directive documentation). However, it may be that your headers checker is showing only the "final" redirect.

If you are using an on-line headers checker, I'd suggest using the "Live HTTP Headers" add-on for Firefox and Mozilla-based browsers instead.

Your code is correct and should work as you desire. However, do be sure that you are not mixing mod_alias with mod_rewrite directives; Doing so can result in the two modules' directives executing in an unexpected order.

If you *are* using mod_rewrite, then an interaction with existing internal rewrites is a distinct possibility: Change your new redirect to use a mod_rewrite RewriteRule directive instead of a mod_alias Redirect directive, and be sure that all of your external redirect rules are placed before any internal rewrite rules (This applies across all config and .htaccess file in all directories, BTW). More on that important and relevant subject in our Library here [webmasterworld.com].

Also, if the naming between your old and new URLs is consistent, then you should only really need one redirect rule to handle all of them. Use the power of regular expressions pattern-matching to best advantage...

In blog.example.com/.htaccess, something like:

RewriteRule ^([^/.]+)\.html$ http://www.example.com/blog/$1 [R=301,L]

This redirects all top-level-directory .html files on blog.example.com to the same-named page in the /blog subdirectory on www.example.com

Note that in moving to IIS, you will now have to pay for the ISAPI Rewrite add-on module in order to get the same level of functionality as provided free by mod_rewrite on Apache... :(

Jim

Esoos

7:56 pm on Aug 25, 2010 (gmt 0)

10+ Year Member



Thanks for the excellent reply, Jim.

Looks like I found the issue, pretty basic oversight on my part. I had the redirection for the root of the blog set up like:

Redirect 301 / http://www.example.com/blog/

Which was redirecting the stems of all the URLs without changing them. I changed it to:

Redirect 301 /index.php http://www.example.com/blog/

...which I believe solved the problem.

Thanks again, really appreciate the help.

g1smd

10:56 am on Aug 26, 2010 (gmt 0)

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



Note the comment above about using only RewriteRule for all of the rules.

Make sure that if any of your rules use RewriteRule, that you use RewriteRule for all of your rules.

That is, if you use RewriteRule for anything, do not use Redirect or RedirectMatch at all.

Esoos

2:49 pm on Aug 26, 2010 (gmt 0)

10+ Year Member



Interesting. As you probably know, WordPress comes with the following code in its .htaccess file:


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


Since it does use
RewriteRule
, does that that preclude the use of
Redirect
anywhere else in the .htaccess file?

g1smd

4:20 pm on Aug 26, 2010 (gmt 0)

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



Yes. Use only RewriteRule.

That default Wordpress code is also very very inefficient. Replace it with jdMorgan's updated code, which has been posted several times in recent months here in this forum.

jdMorgan

8:17 pm on Aug 26, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Please read the thread that I cited in my post above. It contains important information for you, and would have saved you the trouble of posting your last two posts...

Jim