gold_member
gold_member

Reputation: 37

Remove .html and .php extensions from the URL using OpenBSD's httpd.conf file

This might be more of a regex question or maybe a httpd.conf but I think I'm pretty close. Just need a bit of help. Let me say, I always struggle with regex, despite websites like this: https://regex101.com.

I need help with rewriting URLs by removing the .html and .php. In my web server, I have a few HTML pages and one PHP page.

In Apache, my previous web server, I had the below listed in my .htaccess file and it worked perfectly.

RewriteEngine On
RewriteBase /
# **PHP Files** - Redirect file.php to file
RewriteCond %{THE_REQUEST} \s/([^.]+)\.php [NC]
RewriteRule ^ /%1 [NE,L,R]
# Internal map file.php to file
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)/?$ /$1.php [L]

# **HTML Files** - Redirect file.html to file
RewriteCond %{THE_REQUEST} \s/([^.]+)\.html [NC]
RewriteRule ^ /%1 [NE,L,R]
# Internal map file.html to file
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*)/?$ /$1.html [L]

I would reference URLs as such: www.example.com/about or www.example.com/contact-us (no .html or .php extensions were given in my ahref) and it would rewrite the URL without the extension.

Under my new web server, I have my httpd.conf configured as such:

server "example.com" {
    listen on * tls port 443
    root "/htdocs/example.com"
    hsts
    directory index index.html
    tls {
        certificate "/etc/ssl/mycert.pem"
        key "/etc/ssl/private/mykey.key"
    }
    location "/*.php" {
        fastcgi socket "/run/php-fpm.sock"
    }
    location "/*.php*" {
        fastcgi socket "/run/php-fpm.sock"
    }
    # Remove .html extension
    location match "/([^.]+)$" {
        request rewrite "/%1.html"
    }
}

The last "location match" highlighted in bold rewrites all of my .html to files without an extensions. It works as expected for all pages that are in HTML. However, the one PHP page that I have, it doesn't work, obviously. I get a 404 error because the server is expecting a contactus.html but there is contactus.php instead.

I need a regex so when the URL is https://example.com/contactus.php it would rewrite it as https://example.com/contactus.

I have tried the following, but none seem to have worked:

location match "/([^.]+)\.html$" {
    request rewrite "/%1.html"
}
location match "/([^.]+)\.php$" {
    request rewrite "/%1.php"
}
location match "/(%w+).html$" {
    request rewrite "/%1.html"
}
location match "/(%w+).php$" {
    request rewrite "/%1.php"
}

Note: OpenBSD has a doc on patterns, hence %w. Reference: https://man.openbsd.org/patterns.7

location match "^/(.*)(\.html)$" {
    request rewrite "/$1"
}
location match "^/(.*)(\.php)$" {
    request rewrite "/$1"
}

I've read through these pages:

  1. https://daemonforums.org/showthread.php?t=11615

  2. https://man.openbsd.org/patterns.7

  3. https://marc.info/?l=openbsd-tech&m=152763061916519&w=2

And a ton of Googling but my server just doesn't work with both.

Please note, after each change to the config file, I restarted the httpd service:

rcctl restart httpd

Is there a specific regex I can use to tackle both page types?

Thank you!

Upvotes: 1

Views: 188

Answers (1)

gold_member
gold_member

Reputation: 37

I think I figured it out. At least, the following works for me. I am not sure if there is a better solution to this or one that's more efficient but thought I share what I have so far.

location "/contact-us" {
    request rewrite "/contact-us.php"
}
location match "/([^.]+)$" {
    request rewrite "/%1.html"
}

The order is important here. If a specific page is hit, in this case the Contact-Us page, it will rewrite as contact-us.php. Otherwise, for everything else, it will rewrite it with a .html extension. The $ sign at the end of the regex is very important so that css, js and image files do not get picked up by this rewrite.

If anyone knows how to change the first block from location "/contact-us" to location match (<some_regex>.php) so that I don't have to hard-code individual PHP pages, I would appreciate the help.

Upvotes: 0

Related Questions