Welcome to WebmasterWorld Guest from 54.196.244.206

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

Removing .html from URL

     
7:09 pm on May 9, 2013 (gmt 0)

New User

5+ Year Member

joined:June 20, 2006
posts: 38
votes: 0


I want to add an entry to my wordpress blog's .htaccess file to redirect users coming to URLs like

example.com/2013/01/post-name.html To

example.com/2013/01/post-name/
7:37 pm on May 9, 2013 (gmt 0)

Junior Member

joined:Apr 6, 2013
posts:149
votes: 0


This should do the trick.

RewriteRule ^(.*)\.html$ /$1/ [R]


Consider checking through the rewrite documentation to find out about other cool stuff you can do.

[httpd.apache.org...]
8:12 pm on May 9, 2013 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month

joined:Apr 9, 2011
posts:12696
votes: 244


Yup. Common question. What have you tried so far? Remember that you need both halves: the redirect to change the URL displayed in the user's browser, and then a rewrite to fetch the content from wherever it "really" lives.

You've been around since 2006, so you know nobody is going to write it out for you.

But before you start...

The thread title says "removing html". Going extensionless is popular. But the content of your post says something slightly different: Now you're talking about redirecting from the requested filename, like
filename.html
filename2.html
to a pseudo-directory
filename/
filename2/
as if each individual post lived in a directory of its own:
example.com/more-stuff-here/filename/index.html
example.com/more-stuff-here/filename2/index.html

It isn't flat-out wrong, but it seems unnecessary. Before you start laying out your rules, make sure you've settled on the official form of your page name.
8:41 pm on May 9, 2013 (gmt 0)

Senior Member

WebmasterWorld Senior Member drdoc is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 15, 2002
posts:6807
votes: 0


There's also content negotiation ...

If you simply want example.com/foo.html to look prettier (example.com/foo), then there's no need for redirects. Simply let Apache handle it for you via content negotiation.

Now, like lucy24 said, if there's more to it than that (in addition to removing .html from the url you want to direct your request to a file with a different name, or within a different folder), then redirects may be required.
9:19 pm on May 9, 2013 (gmt 0)

New User

5+ Year Member

joined:June 20, 2006
posts: 38
votes: 0


Thanks guys for help.

I tried already, but it didnt work i am unable to understand why.

RewriteRule ^(.*)\.html$ /$1/ [R]

No I dont just want prettier URLs the purpose is to let search engines and users know about the new location.
9:40 pm on May 9, 2013 (gmt 0)

Junior Member

joined:Apr 6, 2013
posts:149
votes: 0


> No I dont just want prettier URLs the purpose is to let
> search engines and users know about the new location.

The rewrite rule I showed should do exactly that. The
[R]
means redirect. (By default, Apache will send a 302 temporary redirect. If this new location is permanent, then you can change it to
[R=301]
to send a permanent redirect.)
10:38 pm on May 9, 2013 (gmt 0)

New User

5+ Year Member

joined:June 20, 2006
posts: 38
votes: 0


Dideved I know it is supposed to redirect but its not working.
11:18 pm on May 9, 2013 (gmt 0)

Junior Member

joined:Apr 6, 2013
posts:149
votes: 0


Ahh, I forgot the "last" flag.

RewriteRule ^(.*)\.html$ /$1/ [R,L]
11:22 pm on May 9, 2013 (gmt 0)

New User

5+ Year Member

joined:June 20, 2006
posts: 38
votes: 0


I remembered the L flag and added it :) it didnt work.
11:57 pm on May 9, 2013 (gmt 0)

Junior Member

joined:Apr 6, 2013
posts:149
votes: 0


Can you post your full htaccess? This redirect works on my local server, so my best guess is that there's something else conflicting with it.
12:12 am on May 10, 2013 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month

joined:Apr 9, 2011
posts:12696
votes: 244


its not working.

Uh-oh, the dreaded "it's not working". What, if anything, happens when you try the rule?

RewriteRule ^(.*)\.html$ /$1/ [R]

Urk. Lots of problems there.

99 times out of 100, you want
RewriteRule ^([^.]+)\.html http://www.example.com/$1 [R=301,L]

But let's not go there.

For starters: [R] refaults to 302, a temporary redirect. (Same applies to redirects done via mod_alias, "Redirect" by that name.) If you've permanently changed the page name, you need R=301. This won't affect individual users accessing the file, but apparently makes a big difference in some search engines. It also affects responses from the w3c link checker. Don't know about Xenu.

The second problem is that you don't say what happens after
www.example.com/filename/

Does the content in fact live at
www.example.com/filename/index.html (or .php or whatever you normally use)? If not, you now need a second rule to rewrite-- not redirect-- the new URL to the physical location of the content.

:: break here for change in target of "you" ::

Simply let Apache handle it for you via content negotiation.

Can you elucidate? Usually when you think of mod_mime and/or mod_negotiation activity you think of jiggery-pokery involving extensions. But here-- assuming no / slash --there is no extension.

:: shuffling papers ::

Are we here [httpd.apache.org]?
to use this feature, you must have a handler set in the configuration that defines a file suffix as type-map; this is best done with

AddHandler type-map .var


in the server configuration file.

It kinda looks as if this directive has to go in the config file --unlike vanilla AddHandler, which can be used in htaccess.

Besides, the details are giving me a headache. Maybe you meant MultiViews, two paragraphs further along. This is less headachy and can definitely be set in htaccess-- but seems like it would significantly slow down the server. That is, more than a RewriteRule, which gives you an immediate one-on-one match without having to look up anything.
1:36 am on May 10, 2013 (gmt 0)

Junior Member

joined:Apr 6, 2013
posts:149
votes: 0


> 99 times out of 100, you want
> RewriteRule ^([^.]+)\.html http://www.example.com/$1 [R=301,L]
> But let's not go there.

How classy of you to go there, then say "let's not go there." ;)

0 times out of 100, you want this. It's neither simpler, nor more efficient, nor will it correctly match all URLs.
1:42 am on May 10, 2013 (gmt 0)

Junior Member

joined:Apr 6, 2013
posts:149
votes: 0


The second problem is that you don't say what happens after
www.example.com/filename/

Does the content in fact live at
www.example.com/filename/index.html (or .php or whatever you normally use)? If not, you now need a second rule to rewrite-- not redirect-- the new URL to the physical location of the content.


The OP didn't explicitly specify, but since he's using WordPress, I suspect these are all "virtual" URLs.
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members