Forum Moderators: phranque

Message Too Old, No Replies

Wildcards in htaccess to remove directory and truncate file type

         

Notorious ROI

8:08 pm on Sep 8, 2011 (gmt 0)

10+ Year Member



Ported a blog into WordPress. However, WP trunciated URLs in two ways (both good!). Is there a wildcard (* or something) that will 301 across hundreds of files.

For example

blog.example.com/2011/09/06/blog-post/

was

blog.example.com/extra_directory/2011/09/06/blog-post.html

Thus, I need to wildcard both the /extra_directory/ (remove) and the .html to /

Any of you fine folks have a solution I can try?

Thanks,
- John

lucy24

9:13 pm on Sep 8, 2011 (gmt 0)

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



Is it always "extra_directory" or can different things occupy this location? At a minimum, is it always exactly one directory? If so, the pattern will look like

[^/]+/([^.]+)\.html

The directory isn't captured because you will be throwing it away; the rest of the address up to the html is captured so you can reuse it with $1.

Rewrite and Redirect are different things, but both can be achieved with the same rule. Search the Apache forum and you will find several hundred explanations; I believe g1smd has boilerplate for the purpose.

Just don't ask anyone to write your RewriteRule froms scratch, or we'll get annoyed ;)

Notorious ROI

10:12 pm on Sep 8, 2011 (gmt 0)

10+ Year Member



Oooh, we're close.

Yes, the pattern is consistent.

I see you code example, but can I use 'as is' in my htaccess?

lucy24

10:36 pm on Sep 8, 2011 (gmt 0)

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



You can if you know exactly where to plug it in ;)

[^/] means "stop when you hit the first slash" -- in other words, this is the first directory.

[^.] means "stop when you hit the first period" -- that is, before you get to the .html extension. That's assuming you don't have something horribly ill-advised like urls containing periods.

Notorious ROI

10:39 pm on Sep 8, 2011 (gmt 0)

10+ Year Member



Awesome!

Haha, no worries. I've been a part of WebmasterWorld since '02. Just a diff name.

Let me give it a whirl.

Thx Lucy