4

I have trying to figure out how to get working in subfolder "admin" the same thing which works in root dir, problem is I don't know how to do that.

Currently I have:

RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ?arg=$1 [L,QSA]

# /
RewriteCond %{REQUEST_URI} !\.[[:alnum:]]+$
RewriteRule ^(.+[^/])$ /$1/ [R=301]

Now it works like: http://example.com/page translate into http://example.com/?arg=page

What I would like to do in addition is this: http://example.com/admin/page translate into http://example.com/admin/?arg=page

I don't know at all how to make that happen, can somebody help me please?

Thanks

user1085907
  • 1,009
  • 2
  • 16
  • 40
  • you can save yourself a lot of hassle by using a routing-lib like [slim](http://www.slimframework.com/) or [klein](https://github.com/chriso/klein.php) – birdspider Mar 02 '16 at 20:54

2 Answers2

4

You can use these rules with appropriate RewriteBase:

RewriteEngine On
RewriteBase /admin/

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule [^/]$ %{REQUEST_URI}/ [L,R=301,NE]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ?arg=$1 [L,QSA]
anubhava
  • 761,203
  • 64
  • 569
  • 643
3

Or just add

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/admin/(.*)$ admin/index.php?arg=$1 [L,QSA]
Muhammed
  • 1,592
  • 1
  • 10
  • 18
  • Whats not working? This is the correct rule. Do you have index.php in admin/index.php? Put var_dump($_GET['arg']) there, you should see it – Muhammed Mar 02 '16 at 21:00
  • Problem is that I use same thing on root folder, so I get redirection to root folder from admin, it using arg=/admin/page instead example.com/admin/?arg=page – user1085907 Mar 02 '16 at 21:09
  • My new rule will not work on root folder, only on admin folder. Put this rule first and then your other rule. Or better just have everything routed to a main file, say app.php, and handle routing from there accordingly. – Muhammed Mar 02 '16 at 21:13
  • I get correct solution from anubhava, anyway thanks for help! – user1085907 Mar 02 '16 at 21:14