homepage Welcome to WebmasterWorld Guest from 54.204.215.209
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Accredited PayPal World Seller

Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
Redirecting .html file to new location (extentionless)
Redirecting .html file to new location (extentionless)
jabz




msg:4307161
 3:14 am on May 3, 2011 (gmt 0)

Hi,

I have a problem, redirecting requests to a former .html file to the new location of that file (which is now exentionless).

For the sake of finding a solution, I have set up a small testing environment.
Old .html-File: /sub1/sub2/html.html
New Location: /sub3/new (supposedly)

When I request the old file, I always get an 404 Error. I'm kinda stuck here and hope you can help me out... that's what I got in my .htaccess:

RewriteEngine on

# old to new
redirect 301 /sub1/sub2/html.html http://uiqe.com/sub3/new

# redirect all non-canonical domains to canonical domain
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^uiqe\.com$
RewriteRule ^(.*) http://uiqe.com/$1 [R=301,L]

# trailing slash
RewriteCond %{REQUEST_URI} !(.*)/$

# directory index
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*index\.html\ HTTP/
RewriteRule ^(.*)index\.html$ http://%{HTTP_HOST}/$1 [R=301,L]

# force extensionless url's
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*[^/.]+\.html\ HTTP/
RewriteRule ^(([^/]+/)*[^/.]+)\.html$ http://%{HTTP_HOST}/$1 [R=301,L]


Thank you.

 

g1smd




msg:4307196
 7:07 am on May 3, 2011 (gmt 0)

The rules are in the wrong order. In particular the non-www to www rule must be LAST in order to avoid generating a multi-step redirection chain for some non-canonical requests.

The non-www rule can be simplified slightly by combining the two RewriteCond lines and using !^(example\.com)?$ for the RegEx pattern.

The "slashes" rule appears to be incomplete?

In the index rule, the .* pattern is entirely inappropriate. Use ([^/]+/)* or similar here.

In the index and extensionless rules state the actual domain name in the target, and not just HTTP_HOST, so that if those types of request are non-canonical, the canonical domain is forced at the same time as fixing the other problem. This avoids a multiple-step redirection chain.

Never use the Redirect directive if you have other rules using RewriteRule. Use RewriteRule for all of the directives.

jabz




msg:4307390
 3:50 pm on May 3, 2011 (gmt 0)

Thank you!
I gave my best to understand and follow your tips.

I have deleted the obsolete RewriteCond line in the non-www rule.

You were right, the slashes rules was incomplete. Should now be complete.

I have also changed the pattern rule for index files. Your solution should be sufficient for what I want to do. Thank you.

I have also changed the rule order. That's what I have now:

RewriteEngine on
RewriteBase /

# always add a trailing slash to folders
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*[^/])$ /$1/ [R=301,L]

# rewrite index.html to folder (directory index)
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*index\.html\ HTTP/
RewriteRule ^(([^/]+/)*)index\.html$ http://uiqe.com/$1 [R=301,L]

# old to new (UNTOUCHED)
redirect 301 /sub1/sub2/html.html http://uiqe.com/sub3/new

# force extensionless url's
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*[^/.]+\.html\ HTTP/
RewriteRule ^(([^/]+/)*[^/.]+)\.html$ http://uiqe.com/$1 [R=301,L]

# redirect all non-canonical domains to canonical domain
RewriteCond %{HTTP_HOST} !^uiqe\.com$
RewriteRule ^(.*) http://uiqe.com/$1 [R=301,L]


The redirect is still not working, of course, I haven't touched it.

I have trouble understanding how I can do the redirect using RewriteRule. I have several files which changed their location and there is absolutely no pattern I can come up with to catch them all. :(
Can't I redirect them one by one (only about 11 files)?

How would you do that? How would a RewriteCond look like, how the RewriteRule?
Stuck.

Thank you for your help. I really appreciate you are investing your time, helping me out.

g1smd




msg:4307501
 8:08 pm on May 3, 2011 (gmt 0)

In the trailing slash rule
^(.*[^/])$ simplifies to !/$ here. The target should also include the protocol and domain name.

There's still one
.* pattern to fix in the "index" rule.

Redirect 301 /sub1/sub2/html.html http://uiqe.com/sub3/new
becomes
RewriteRule ^sub1/sub2/html\.html http://uiqe.com/sub3/new [R=301,L]

The RewriteCond line in the non-www rule that you deleted isn't obsolete until you also add the ( ) and ? to the existing RewriteCond.

Now that you have forced extensionless URLs, I assume there must be an internal rewrite somewhere to map those URLs back to some internal location inside the server that will serve the content.

jabz




msg:4307758
 12:12 pm on May 4, 2011 (gmt 0)

Thank you for your input. This is what I am using now:

RewriteEngine on
RewriteBase /

# trailing slashes
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^!/$ http://uiqe.com/$1/ [R=301,L]

# rewrite index.html to folder (directory index)
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.html\ HTTP/
RewriteRule ^(([^/]+/)*)index\.html$ http://uiqe.com/$1 [R=301,L]

# old to new
RewriteRule ^sub1/sub2/html\.html$ http://uiqe.com/sub3/new [R=301,L]

# extensionless url's
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*[^/.]+\.html\ HTTP/
RewriteRule ^(([^/]+/)*[^/.]+)\.html$ http://uiqe.com/$1 [R=301,L]

# canonical hostname
RewriteCond %{HTTP_HOST} !^uiqe\.com$ [NC]
RewriteRule ^(.*)$ http://uiqe.com/$1 [R=301,L]


Regarding your comments:
I have simplified the the trailing slash rules. Thank you for that tip.

I have also fixed the .* pattern in the index rule.

The Redirects are now working. Am I right about the missing "$" to end the string? It's missing in your example.

I have also completed the non-www rule using a different approach. To be honest, I do not understand your idea about the () and the question mark. Can you post that, so I can try to understand it? Thanks.

Regarding the extentionless URL's: The files on the server are actually real .html files, located where the link points to (real folders). Yes I have forces extentionless URL's but the URL - including the .html - mirrors the exact location of that file. Have I forgotten anything, do I need some kind of mapping?

Thanks again for your help!

g1smd




msg:4307776
 12:24 pm on May 4, 2011 (gmt 0)

The final $ is optional. If someone were to request example.com/sub1/sub2/page.htmlblahblah then that would also be redirected (retaining the visitor), whereas adding the $ would force the 404 page to show.

You redirect example.com/page.html to example.com/page so what rule allows a request for example.com/page to be fetched from the /page.html location inside the server?

jabz




msg:4307797
 12:50 pm on May 4, 2011 (gmt 0)

Regarding the "$": Thanks for the clarification.

Regarding the URL mapping: "what rule allows a request for example.com/page to be fetched from the /page.html location inside the server?" Good question. How would I do that? A Google search on "map extentionless url to file on server" did not really help me out. Mod_alias, is that the way to go?

g1smd




msg:4307806
 1:02 pm on May 4, 2011 (gmt 0)

If the site is working when extensionless URLs are requested, then you already have something in place that does this. I was wondering what it was. It would usually be an internal rewrite using another RewriteRule with just the [L] flag.

jabz




msg:4307833
 2:08 pm on May 4, 2011 (gmt 0)

The site is functioning perfectly. I have posted everything but the compression. Strange.

However, thanks a billion for your help. I have learned a lot from you, that's priceless.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About
© Webmaster World 1996-2014 all rights reserved