htaccess multiple directories and GET params

htaccess multiple directories and GET params



I'm developing a site in PHP and running it locally on my machine with MAMP for testing purposes. Right now I'm trying to clean up the URLs for the backend area(admin) to produce URLs like:


localhost/admin/products/read/1



This is my .htaccess in root:


.htaccess


RewriteEngine On
RewriteCond %REQUEST_FILENAME !-d
RewriteCond %REQUEST_FILENAME !-f
RewriteRule ^([^.]+)$ $1.php [NC]
RewriteRule ^admin/products/read/([0-9]+)/?$ admin/products/read.php?id=$1 [NC]



I've also been trying this RewriteRule whilst removing the last line of the code above:


RewriteRule


RewriteRule ^admin/(products|users|companies)/(read|update|delete)/(.*)$ /$1?id=$2 [NC,L]



Folder structure:


htdocs
- admin
- products
- read.php
- update.php
- delete.php
- index.php
- users
- ...
- companies
- ...
index.php



Chrome and Safari are throwing 404 not found when trying URLs such as


localhost/admin/products/read/1
localhost/admin/products/read/1/
etc..



All help is appreciated!



Update 31/08/18



With MrWhite's help I scribbled together this, from his answer, to expand the scope in my .htaccess file:


.htaccess


RewriteRule ^admin/(.*)/(.*)/([0-9]+)/?$ admin/$1/$2.php?id=$3 [L]



Which got me thinking, could I keep the config file cleaner by just checking for the query ID and then rewrite?


ID





"could I keep the config file cleaner by just checking for the query ID and then rewrite?" - Not quite sure what you mean? Only you know what URLs you are trying to catch. But if you are only rewriting a handful of URLs (eg. read, update, delete, index?) and they all follow the same pattern as above then you may not need the catch-all rewrite that follows?
– MrWhite
Aug 31 at 0:41


ID


read


update


delete


index




1 Answer
1



You've got your directives in the wrong order. You are also missing some L flags. And you need to make sure that MultiViews is not enabled.


L


MultiViews



Try the following instead:


Options -MultiViews
RewriteEngine On

RewriteRule ^admin/products/read/([0-9]+)/?$ admin/products/read.php?id=$1 [L]

RewriteCond %REQUEST_FILENAME !-d
RewriteCond %REQUEST_FILENAME !-f
RewriteRule ^([^.]+)$ $1.php [L]



By having the directives the other way round, it would be appending the .php extension to the URL-path and never matching your second directive.


.php



Also, once the directive has matched, you don't want processing to continue so you need the L (last) flag.


L


last





Your updated generalised directive looks OK. Although, with any regex, it is preferable to be as specific as possible. So, for example, if the second path segment should only be lowercase a-z, then change (.*) to something like ([a-z]+).
– MrWhite
Aug 31 at 0:31


(.*)


([a-z]+)





Thanks for the edit on my post, I will apply your input on any further Stackoverflow posts! I also updated my question since I want the RewriteRule to have a broader scope.
– Alex
Aug 31 at 0:52



Required, but never shown



Required, but never shown






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế