homepage Welcome to WebmasterWorld Guest from 54.161.214.221
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
Redirect html pages with htaccess
avatar77




msg:4501986
 12:58 pm on Sep 30, 2012 (gmt 0)

Hi everybody,
i'm not an expert of .htaccess files and i need your help.
This is the .htaccess file i have in my root:

#Options +FollowSymLinks +ExecCGI

<IfModule mod_rewrite.c>
RewriteEngine On

## uncomment the following line, if you are having trouble
## getting no_script_name to work
RewriteBase /

## remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^(.+)/$
RewriteRule ^(.+)/$ $1 [R=301,L]

## turn off index.html, it is for offline mode
RewriteRule ^.*index.html$ index.php [QSA]

## no, so we redirect to our front web controller
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>

Now, i need to redirect permanently all my old html pages to my Home page (i.e. www.mysite.com).
Could you tell me how and when i have to put the redirect command in my .htaccess file?

Thanks in advance

 

g1smd




msg:4501988
 1:07 pm on Sep 30, 2012 (gmt 0)

What code have you tried already for this? You'll need a RewriteRule before all others to do it. However, it's usually bad form to mass redirect multiple URLs to one URL, especially if it's the root page of the site.

The canonical URL for the site root ends with a trailing slash.

The $1 all on it's own in an existing rule target opens your server to path injection. You need to prefix this with the protocol and canonical hostname.

Dump the ifModule container tags.

You need another redirect after your last redirect and before your first rewrite. This should redirect non-www requests to www.

RewriteRule ^.*index.html$ index.php [QSA]

Replace the leading .* with a more specific pattern. Escape the literal period in the index.html part of the pattern. This rule also needs the L flag.

redirect to our front web controller

This is not a redirect. It is an internal rewrite. Amend the comment.

avatar77




msg:4501996
 2:01 pm on Sep 30, 2012 (gmt 0)

So, should i have to modify it like this?

#Options +FollowSymLinks +ExecCGI

<IfModule mod_rewrite.c>
RewriteEngine On

## uncomment the following line, if you are having trouble
## getting no_script_name to work
RewriteBase /

## remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^(.+)/$
RewriteRule ^(.+)/$ $1 [R=301,L]

## turn off index.html, it is for offline mode
RewriteRule ^.*index.html$ index.php [QSA]
RewriteRule ^contacts.html$ /#/contacts/ [QSA]
RewriteRule ^about_us.html$ /#/about_us/ [QSA]
RewriteRule ^services.html$ /#/services/ [QSA]

## internal rewrite
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>

Thanks in advance

incrediBILL




msg:4502105
 7:12 pm on Sep 30, 2012 (gmt 0)

## internal rewrite
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>


The problem with this code which WordPress uses is it's completely dain bramaged and processes ALL files (images, etc.) using the !-d and !-f commands which are inefficient.

See this thread for an optimal version that speeds up the site:
[webmasterworld.com...]

lucy24




msg:4502163
 9:53 pm on Sep 30, 2012 (gmt 0)

## turn off index.html, it is for offline mode

This is obscure. Did your site at some time in the past really use the .html extension-- or did you just use it yourself for local testing? If the pages never existed "in the world" onthe live site, you needn't bother about them.

RewriteRule ^.*index.html$ index.php [QSA]

Every time I think some prepackaged boilerplate has reached its limit and can't get worse, I am proven wrong.

^ = the request has a beginning
.* = there may or may not be something after the beginning of the request
index.html$ = the request ends in "indexXhtml" where X is any one character-- and the server, which has previously captured the entire request, now has to backtrack over and over until it either picks up "indexXhtml" or establishes that it doesn't exist. (Apache operates in one dimension. It can't see that "i" is followed by "n" and so on.)

index.php = no matter what index page the request was for, serve the content that lives at the site's front page
[QSA] = keep the original query string-- which would have happened anyway by default, because the Rule doesn't say anything about making a new one.

[] = and now continue to the next RewriteRule, because there was no [L] flag.

Start by dumping the <IfModule... envelope. Not its content, just the envelope itself. That's easy, and you will have accomplished something.

avatar77




msg:4504533
 3:59 pm on Oct 5, 2012 (gmt 0)

I begin with thank you all for your answers and I apologize for the delay, work problems ...!
I imagined to seek help from professionals like you and here comes my difficulty in putting your valuable advice :-)
Unfortunately my knowledge of. Htaccess is not good.
Let's see if I can find the right code to use.
As I said, I need to optimize my .Htaccess file and modify it by "Redirect" the existing Html pages to my home page (for example) or other flash pages.
I'm upgrading from a site built in "pure html+css" to a one in flash.
So...

#Options +FollowSymLinks +ExecCGI

RewriteEngine On

RewriteBase /

## remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^(.+)/$
RewriteRule ^(.+)/$ $1 [R=301,L]

## Redirect all my html pages to new flash pages
RewriteRule ^index.html$ /#/splash-page/ [QSA]
RewriteRule ^contacts.html$ /#/contacts/ [QSA]
RewriteRule ^about_us.html$ /#/about_us/ [QSA]
RewriteRule ^services.html$ /#/services/ [QSA]

## internal rewrite
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,L]

How would you modify it to make it functional?
I thank you and i hope to get an help from you, again!

avatar77




msg:4505778
 6:37 am on Oct 9, 2012 (gmt 0)

any suggestion?
Thanks

lucy24




msg:4505782
 7:33 am on Oct 9, 2012 (gmt 0)

Don't look at me. I took one look at the hash thingies /#/ and fled. Someone hereabouts apparently understands how they work, though, so sit tight.

Meanwhile: QSA is unnecessary in all your examples. mod_rewrite reappends the query string by default. You only need to specify QSA when you have added a new query and need to reappend the old one.

Besides, what are html pages doing with query strings? If there wasn't one in the first place, there is twice as much reason not to bother about [QSA].

RewriteCond %{REQUEST_URI} ^(.+)/$
Is this intended to mean "the requested URI ends in a slash"? Since you're not capturing, all you need there is
/$
But where is this happening? Is it just insurance against mistyped requests for extensionless URLs, or is there something else going on? Why do you need to look at the request? The only time the server would add a slash to a slashless request is when the URL in question really is a directory-- and then it's a redirect, not a rewrite. It's not like "index.html" where the server quietly adds the name without changing the visible URL.

phranque




msg:4505856
 9:29 am on Oct 9, 2012 (gmt 0)

welcome to WebmasterWorld, avatar77!


## Redirect all my html pages to new flash pages
RewriteRule ^index.html$ /#/splash-page/ [QSA]
RewriteRule ^contacts.html$ /#/contacts/ [QSA]
RewriteRule ^about_us.html$ /#/about_us/ [QSA]
RewriteRule ^services.html$ /#/services/ [QSA]


those look like internal rewrites to fragment identifiers and that won't get you anywhere with a flash app running in a browser.

assuming you meant those to be external redirects, you need to use proper syntax for that, which means specifying the protocol and hostname in the substitution string and also specifying the R=301 flag.

are you interested in SEO for your site?
if so, i wouldn't recommend using that url structure.
those urls all refer to the same resource with different fragment identifiers.
a search engine won't separately index those urls.
only the root slash will be indexed.
you will essentially be redirecting all those old urls to the home page.

avatar77




msg:4505870
 10:40 am on Oct 9, 2012 (gmt 0)

Meanwhile, thanks for your replies!

>are you interested in SEO for your site?
>if so, i wouldn't recommend using that url structure.

Yes, i'm interested in SEO, for this reason i'm trying to modify the .htaccess file in the right way (but, unfortunately, my knowledge on this is not enough...).
What kind of structure should i use to be sure that a search engine will index my new pages? (they are in the same domain)

RewriteRule ^contacts.html$ [mysite...]
RewriteRule ^about_us.html$ [mysite...]
##do this for each HTML pages to redirect them permanently?
....
....

or it is better a syntax like this...:

Redirect 301 /about_us.html [mysite...]
...
I hope to get because i need a solution from you!

phranque




msg:4505879
 11:26 am on Oct 9, 2012 (gmt 0)

IMPORTANT: Please Use Example.com For Domain Names in Posts:
http://www.webmasterworld.com/apache/4452736.htm [webmasterworld.com]

don't use the Redirect directive as this will mix mod_alias with mod_rewrite directives which can have unpredictable results.


regarding your url structure - that should probably be a separate discussion for another forum.
in general:
- you need a url that has the unique address of the resource to the left of the hash mark.
- why not use the current urls?
the flash program can examine the url of the current page and present the proper content accordingly.


this raises yet another accessibility- and SEO-related issue for another forum.
how is google going to index your flash content?
are you providing alternate content for flash-disabled user agents?

lucy24




msg:4506071
 7:29 pm on Oct 9, 2012 (gmt 0)

:: returning from Forums search ::

Are we talking Ajax or fragments? I gather that Ajax is #! ("hashbang") so that leaves fragments. Those won't work in external redirects. The alternative is query strings, but either way you're ending up on the same page. Unless, ugh, you make the query string part of the URL.

Can we backtrack? What's wrong with your existing URLs and why do you want or need to redirect them? When you say "redirect" do you mean redirect (changed URL) or do you really mean "rewrite" (same URL, variable content)?

"Why" questions are best asked in g###-related forums. Apache is for the "how" questions. (The "who" questions go in SSID and the "where" questions go in-- oh, never mind :) )

avatar77




msg:4506321
 6:58 am on Oct 10, 2012 (gmt 0)

>What's wrong with your existing URLs and why do you want >or need to redirect them?

Because i'm modifying the entire structure of the site.
I'm moving from html+css site to Flash CMS (with different pages and different structure).
I don't need my old html pages anymore but i do not want to lose my pagerank acquired until now.

>When you say "redirect" do you
>mean redirect (changed URL) or do you really >mean "rewrite" (same URL, variable content)?

I mean, exactly, changed URL, so "redirect".
I have, until now, pages like:
index_en.html
index_es.html
contacts.html
services.html
....and so on...

Now, i need to insert the right code in the .htaccess to "redirect" those pages directly to the new pages if users try to access to the site from the old URLs.

I hope i've been a little more clear.
Thanks again and i'll wait your suggestions

g1smd




msg:4506322
 7:01 am on Oct 10, 2012 (gmt 0)

I'm moving from html+css site to Flash CMS

You've just made your site non-workable on most mobile devices and invisible to most search engines.

avatar77




msg:4506325
 7:14 am on Oct 10, 2012 (gmt 0)

>You've just made your site non-workable on most mobile devices and invisible to most search engines.

Not at all.
It's a CMS that is able to "redirect" automatically the mobile devices and pc without Flash player installed to an html version of the site.

lucy24




msg:4506350
 8:22 am on Oct 10, 2012 (gmt 0)

I think we're moving into "Just show him how to aim the ### gun" territory ;)

So what are we talking about here? Some finite number of pages that will each get individual redirects? Or groups of pages whose old URLs fit into a pattern that will have some equivalent in the new URL?

Can you even slap /#/ into the middle of a target? Or does it have to say /%23/ from the get-go?

phranque




msg:4506353
 8:50 am on Oct 10, 2012 (gmt 0)

It's a CMS that is able to "redirect" automatically the mobile devices and pc without Flash player installed to an html version of the site.

is that a redirect or a "redirect"?
=8)

what exactly do you mean?

does it serve alternate content at the requested url for non-flash devices?

or does it 301 to the non-flash content?
what do those urls look like?
do you link to those urls internally?

how does it recognize mobile or non-flash devices?
this may be the content the search engines will crawl and index.
or it might look like cloaking...

avatar77




msg:4506404
 11:01 am on Oct 10, 2012 (gmt 0)

>So what are we talking about here? Some finite number of >pages that will each get individual redirects? Or groups >of pages whose old URLs fit into a pattern that will have >some equivalent in the new URL?
For istance, my site has 10 html pages (index.html, contacts.html, services.html....and so on) and it has been online for 3 years.
Now, i have to delete the old site and put the new one but i need that if the users try to access with the old URL (example.com/index.html) the .htaccess redirect them directly to the new page (example.com//#/splash-page/).
And i have to do this for the all the old pages.
I would like just to know if, in your point of view, this is possible and which is the right syntax...
Thanks for your patience :-)

g1smd




msg:4506587
 6:46 pm on Oct 10, 2012 (gmt 0)

splash-page

Oh dear. I've heard enough.

lucy24




msg:4506655
 10:23 pm on Oct 10, 2012 (gmt 0)

Oh dear. I've heard enough.

See previous post from me in this thread ;)

>So what are we talking about here? Some finite number of
>pages that will each get individual redirects? Or groups
>of pages

Gosh, you have a weird way of quoting. Is that your current screen width? Or text size?

10 pages? That's all? Sounds like all you need is 10 explicit redirects then.

RewriteRule oldname\.html http://www.example.com/newname [R=301,L]

et cetera nine more times. That's assuming the internet-at-large won't wig out over the medial /#/ in the new URLs. Doesn't seem a really sharp idea on the CMS maker's part.

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.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved