homepage Welcome to WebmasterWorld Guest from 54.166.65.9
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

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

Apache Web Server Forum

    
Removing .html from URL
noumaan204




msg:4572435
 7:09 pm on May 9, 2013 (gmt 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/

 

Dideved




msg:4572461
 7:37 pm on May 9, 2013 (gmt 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.

https://httpd.apache.org/docs/2.4/rewrite/

lucy24




msg:4572474
 8:12 pm on May 9, 2013 (gmt 0)

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.

DrDoc




msg:4572479
 8:41 pm on May 9, 2013 (gmt 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.

noumaan204




msg:4572496
 9:19 pm on May 9, 2013 (gmt 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.

Dideved




msg:4572503
 9:40 pm on May 9, 2013 (gmt 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.)
noumaan204




msg:4572518
 10:38 pm on May 9, 2013 (gmt 0)

Dideved I know it is supposed to redirect but its not working.

Dideved




msg:4572524
 11:18 pm on May 9, 2013 (gmt 0)

Ahh, I forgot the "last" flag.

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

noumaan204




msg:4572525
 11:22 pm on May 9, 2013 (gmt 0)

I remembered the L flag and added it :) it didnt work.

Dideved




msg:4572527
 11:57 pm on May 9, 2013 (gmt 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.

lucy24




msg:4572529
 12:12 am on May 10, 2013 (gmt 0)

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.

Dideved




msg:4572540
 1:36 am on May 10, 2013 (gmt 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.

Dideved




msg:4572541
 1:42 am on May 10, 2013 (gmt 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.

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