Forum Moderators: phranque

Message Too Old, No Replies

new webmaster please help with redirect

redirect url to url.html

         

gbgamblers23

10:58 am on Jan 10, 2009 (gmt 0)

10+ Year Member



This is probably something really simple but i'm trying to redirect all urls to the url.html

all my pages are followed by the .html extension, so when someone types in mysite.com/page it says page does not exist, so i need my site to automatically add the .html so it returns as mysite.com/page.html

i figured i could do 301 redirects but having to do one for every page seems really excessive and time consuming. I'm sure theres a mod_rewrite or something that can automatically add the .html extension to all the urls people try to access on my site.

I hope i explained that well, if you have any questions or can help, let me know

Marcia

2:18 pm on Jan 10, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome to the forums, "new webmaster."

It'll serve you well to grasp the concept of how regular expressions work, when you're just starting out, if you'll be using mod_rewrite. Simply put, when a user agent (browser) requests a certain "pattern of characters" for a URL on your site, regular expressions tell the server to deliver or send it to a URL with a different pattern of characters - the one you tell it to in a directive in .htaccess

Here are some threads to look through on regular expressions [google.com].

Also, there are a few basic tutorials in the forum library [webmasterworld.com] that you should find very helpful to get a grounding in the basics.

[edited by: Marcia at 2:25 pm (utc) on Jan. 10, 2009]

g1smd

7:10 pm on Jan 10, 2009 (gmt 0)

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



You surely don't need a redirect. A redirect tells the browser to make a new request for a new URL in a new HTTP transaction.

What you really need is a rewrite. With a rewrite, the browser asks for www.example.com/somefile and the server fetches the content from the file /somefile.html without revealing what the real name of that file actually is.

Does this system need to work only on the root, or does it need to work site-wide into subfolders?

In any case, it is just one or two lines of code to work for the whole lot.

jdMorgan

7:32 pm on Jan 10, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What do the on-page link URLs on your site look like? - Do they have ".html" on them or not?

What is the over-all goal here?

The solutions are different if the goal is to publish and use 'extensionless' URLs, versus simply trying to correct linking errors made by other sites' Webmasters or in forum posts, etc.

Jim

gbgamblers23

9:48 pm on Jan 10, 2009 (gmt 0)

10+ Year Member



thanks for the quick replies

My urls look like [mysite.com...]

The problem is people will try to directly type the page into the address bar as [mysite.com...] , so it wont return my page as it needs the .html extension

All i want to do is redirect all the pages typed in to the correct page ( essentially just adding the .html to the end of the url)

and g1smd, as of now theres no subfolders as it's a small site but if theres a way to make it work in the future if i end up adding subfolders that'de be nice too

Also thanks for the links marcia

jdMorgan

12:37 am on Jan 11, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Something like this should handle it:

# if request filename plus ".html" exists as a file
RewriteCond %{REQUEST_FILENAME}.html -f
# externally redirect to add ".html" to URL-paths without any extension
RewriteRule ^(([^/]+/)*[^./]+)$ http://www.example.com/$1 [R=301,L]

This assumes that you already have other working rules in your .htaccess file.

Jim

g1smd

1:14 am on Jan 11, 2009 (gmt 0)

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



There's a lot of talk about "extensionless" URLs.

Would you consider making the www.example.com/somepage format the 'real' URL format for the pages of the site?

Additionally, how old is the site, and is it already fully indexed by Google, and other search engines?