user3504335
user3504335

Reputation: 177

Htaccess Rewrite Rule with Page Not Found issue

htaccess question on rewrite rules

I have 3 files in my folder

create.php
index.html
index.php

and I got this as my .htaccess for rewrite rules

DirectoryIndex index.php
RewriteEngine On
RewriteBase /admin/

RewriteRule ^([^/]+)/([^/]+)/?$ permissions/index.php?action=$1&id=$2 [L,QSA]


RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]

# Resolve .php file for extensionless php urls
RewriteRule ^([^/.]+)$ $1.php [L]

The issue is I want to access the site

http://member.mydomain.com/admin/permissions/create

But it tell me

The requested URL /admin/create.php was not found on this server.

Is there something wrong with my .htaccess , the rewrite rule is mainly for index.php to do its query sending, I would like my create.php to be accessible with the path of

 http://member.mydomain.com/admin/permissions/create

Thanks !!

Updated .htaccess

DirectoryIndex index.php
RewriteEngine On
RewriteBase /admin/

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

RewriteRule ^([^/]+)/([^/]+)/?$ permissions/index.php?action=$1&id=$2 [L,QSA]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]

But it return same error

Not Found

The requested URL /admin/permissions/create was not found on this server.

Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.

Upvotes: 0

Views: 8704

Answers (1)

Prix
Prix

Reputation: 19528

On your code you have only:

# Resolve .php file for extensionless php urls
RewriteRule ^([^/.]+)$ $1.php [L]

However just that does not take into account weather or not the file actually exists or not before redirecting, you want something like:

# Resolve .php file for extensionless php urls
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/admin/$1\.php -f
RewriteRule /([^/]+)/?$ $1.php [L]

You would also need to place this rule at the top of your rules.

This is the entire set of rules:

Options +FollowSymLinks -MultiViews

DirectoryIndex index.php

RewriteEngine On
RewriteBase /admin/

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/admin/$1\.php -f
RewriteRule /([^/]+)/?$ $1.php [L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?action=$1&id=$2 [L,QSA]

Your do nothing rule is not required.

Upvotes: 5

Related Questions