Home > Mobile >  how do I solve a 404 error from a pretty url?
how do I solve a 404 error from a pretty url?

Time:10-18

I'm trying to redirect my content removing the .php extension from all the files using this .htaccess:

RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [NC,L] 

and when I try to include any other redirect like this one give me a 404 error:

RewriteRule ^create/(.*)$ ./create.php?app=$1 

I'm trying to use something like https://myurl.com/create/37744e17-98ff-58a9-8996-7cf746e508b9 instead of https://myurl.com/create.php?id=37744e17-98ff-58a9-8996-7cf746e508b9, but looks like the start of my .htaccess it's giving the main issue, there's a way to solve this?

CodePudding user response:

You've not stated where you are putting that rule. It would need to go first. The order is important.

Try it like this instead:

# Disable MultiViews
Options -MultiViews

RewriteEngine On

RewriteRule ^create/([a-f0-9-] )$ create.php?id=$1 [L]

# General extensionless ".php" URLs
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule (.*) $1.php [L] 

Note that you used an id URL parameter in your example, but used app in your rule? I've changed this to id.

Based on your example URL, the id URL parameter can consist of the characters a-z, 0-9 and hyphens only.

The RewriteBase directive is not required. The ./ prefix on the substitution string is not required and should be removed.

In your original rule that appends the file extension there is no need to check that the request does not map to a directory before checking that it does map to a file (with a .php extension). You were also potentially checking a different file-path to the one being rewritten to.

Note that you need to ensure that MultiViews is disabled, otherwise create.php is called but without the URL parameter.

  • Related