0

I'm developing a small CMS solution with Perch. It's currently running on WampServer on my local development machine.

As Perch doesnt provide friendly URL's out of the box, I wanted to implement this, whilst ensuring the /perch directory remains untouched.

So far, I have the rewriting part working i.e. a request for /blog.php will 301 to /blog, and, /blog will rewrite to /blog.php, using the rules below:

Options +FollowSymLinks -MultiViews

RewriteEngine On

# Rewrites domiain.com/file to domain.com/file.php
RewriteCond %{REQUEST_URI} !^/perch
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php 

# Redirects domain.com/file.php to domain.com/file
RewriteCond %{REQUEST_URI} !^/perch
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteCond %{REQUEST_URI} ^(.+)\.php$
RewriteRule (.*)\.php$ /$1 [R=301,L]

However, I'm still left with .php extensions in the HTML output. I tried adding the following to my .htaccess file:

AddOutputFilterByType SUBSTITUTE text/html
#Replace all .php extensions
Substitute s|.php||ni
#Original blog pattern /blog/post.php?s=2014-11-18-my-first-blog-post
Substitute s|blog/post\?s=(\w+)|blog/$1|i

However, this is applied globally, i.e. even to links within the /perch folder. I couldn't find anyway of adding a condition to apply it to everything except for the /perch folder - is there such a way?

I also looked at the ProxyPass/ProxyReversePass documentation, but this seems like overkill to just replace some HTML on a page.

Any help would be greatly appreciated.

Kind regards, dotdev

dotdev
  • 543
  • 7
  • 19

1 Answers1

3

Are you talking about the Perch CMS from www.grabaperch.com?

Everything is here: http://docs.grabaperch.com/video/v/simple-url-rewriting/

However, I'm still left with .php extensions in the HTML output

.htaccess / mod_rewrite does nothing to your HTML output.

Think of the RewriteRules as a postman who delivers mail (URLs) to target mailboxes (actual files).

What you do is you "manually" omit the .php extension in your markup (HTML output):

  • In perch_pages_navigation(), you need to set hide-extensionsto true
  • URLs you add manually: just write them without .php

Now you need to instruct the postman to route those addresses to the .php file anyway. That's what these RewriteRules are for. So .htaccess doesn't remove the .php suffix - on the contrary, it adds it.

Here's the basic .htaccess (goes into your public_html directory) for Perch (or any "remove .php" use case) + Perch Blog. I've added some explanations:

# make sure the address we received (e.g. /mypage) is not an existing file      
RewriteCond %{REQUEST_FILENAME} !-f
#  make sure it's not an existing directory either
RewriteCond %{REQUEST_FILENAME} !-d
# make sure there IS an existing .php file corresponding to it
RewriteCond %{REQUEST_FILENAME}.php -f
# if the address starts with "blog/", pick what comes afterwards, put it into the GET Parameter and quit (that's the [L]) 
RewriteRule ^blog/([a-zA-Z0-9-/]+)$ /blog/post.php?s=$1 [L]
# if the first conditions are ok, but it wasn't a blog post (else we would have quit), just append .php to it. Ah, and keep other get params (that's the QSA=Query String Append). 
RewriteRule ^(.+)$ $1.php [L,QSA]

For more refined possibilities, you can e.g. start here: https://github.com/PerchCMS/perchdemo-swift/blob/master/public_html/.htaccess

This will have no impact at all on the functionality of the CMS in /perch/.

Urs
  • 4,984
  • 7
  • 54
  • 116