Need some htaccess help

The background…

I’m building a new RW site for a client who previously had a Wordpress site. The blog has 150 entries, which we are retaining, but only for archive, SEO and old bookmark purposes.

Initially I thought… Set up a WP site, export the posts from the old site, import to the new one, link to new blog only WP site from new RW site. I set this up and it all worked lovely.

Then Jannis had the idea to convert all the old WP blog posts to Poster2 Markdown files. This way, I didn’t need an install of WP for the sole purpose of a blog archive. Long story short on that one… Jannis came up trumps and sent me 150 odd markdown files which work perfectly in P2.

So I now have a page with Poster2 on it, and all the old WP blogs displayed.

The old WP site was setup a bit weirdly, so the URL to the blog page is…

www.domain.com/blog/

but the URL to each posts is…

www.domain.com/blog-post-name/

Note, no /blog/ folder in the post URL and the trailing slash.

The first bit of replicating the old URL is easy: Poster supports pretty URLs, so I used this in the htaccess…

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule blog/author/(.*) /blog/index.php?author=$1 [L,NC,QSA]
RewriteRule blog/category/(.*) /blog/index.php?category=$1 [L,NC,QSA]
RewriteRule blog/date/(.*) /blog/index.php?date=$1 [L,NC,QSA]
RewriteRule blog/search/(.*) /blog/index.php?search=$1 [L,NC,QSA]
RewriteRule blog/tag/(.*) /blog/index.php?tag=$1 [L,NC,QSA]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule blog/(.*) /blog/index.php?post=$1 [L,NC,QSA]

Now, the Poster2 posts have a nice clean “pretty URL” post, like this…

www.domain.com/blog/blog-post-name

Note the /blog/ bit in the URL and the lack of a trailing slash.

Now the fun, and the bit I need help with. There are two things I need to do…

  1. Remove the /blog/ bit form the post URL
  2. Remove the trailing slash from the URL that someone might have bookmarked. So, if the type in (or click a link pointed to) www.domain.com/blog/blog-post-name/ the URL needs to be changed to www.domain.com/blog/blog-post-name

I’ve had some success with this. I found some code that will indeed pull the /blog/ bit from the URL for blog posts, but it was also pulling it from the blog page URL. So if you typed in www.domain.com/blog you ended up at www.domain.com, the code I got also broke other stuff too.

I also found some code to remove the trailing slash, but it was removing it from all URL’s on the site, which broke all other pages!

I’ve been harassing Jannis all afternoon on this, so I feel it’s time to give the poor guy a break and ask someone else, so here I am!

Any htaccess exports able to help?

1 Like

Cracked it!

First added this to remove /blog/

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+blog/([^\s]+) [NC]
RewriteRule ^ %1 [R=301,L]

Then added this to remove trailing slash…

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)/$ /$1 [R=302,NE,L]

This all worked on the blog posts, but broke other pages. Then @jannis suggested excluding the other pages from the rewrites.

So added this…

RewriteRule ^(lilianadabic)($|/) - [L]
RewriteRule ^()($|/) - [L]
RewriteRule ^(wedding-dresses)($|/) - [L]
RewriteRule ^(enzoani)($|/) - [L]
RewriteRule ^(etoile)($|/) - [L]
RewriteRule ^(oscarlili)($|/) - [L]
RewriteRule ^(samplesale)($|/) - [L]
RewriteRule ^(accessories)($|/) - [L]
RewriteRule ^(real-brides)($|/) - [L]
RewriteRule ^(the-boutique)($|/) - [L]
RewriteRule ^(bridal-services)($|/) - [L]
RewriteRule ^(faq)($|/) - [L]
RewriteRule ^(contact)($|/) - [L]

And bingo… It’s all working!

Big shout to Jannis for first converting the WP posts to Poster 2 Markdown files, then for the pointers on the htaccess rewrites.

3 Likes

Nice to see all things come together now. Cheers, welcome!