anon
anon

Reputation:

mod_rewrite: Hiding PHP extension and showing querystrings as directories

I have files with the extension .php and I'd like to hide them, as well as show querystrings as inner directories of the rewritten PHP extension.

In other words, I'd like https://domain.com/page.php

to be accessed with https://domain.com/page/ (with the ending slash) and for

https://domain.com/page/?id=1 to be accessed with

https://domain.com/page/1/ (also with the ending slash)

I'd also like access to the page with the .php extension return a 404 not found.

How would I do this in the .htacess file?

Upvotes: 0

Views: 332

Answers (2)

Lusitanian
Lusitanian

Reputation: 11122

You could try this mod_rewrite rule generator. If you want to write the rules yourself, it's fairly simple: it uses the same regular expressions as the PHP preg_* functions do :)

In your particular instance, use RewriteRule ^([A-z0-9]+)/([1-0]+)/$ $1.php?page=$2

Upvotes: 0

cstrat
cstrat

Reputation: 1047

HTACCESS would need to look something like this:

RewriteEngine On
RewriteRule ^([A-z0-9]+)/$ $1.php
RewriteRule ^([A-z0-9]+)/([1-0]+)/$ $1.php?page=$2

For the custom 404 Error you would use:

ErrorDocument 404 /404/

However you will need to make sure that you apply the first rewrite rule to the 404 page. You will need a 404.php file.

Upvotes: 1

Related Questions