1

On one of my websites the .htaccess is working for links, but the php file doesn't detect a GET method.

My .htaccess

ErrorDocument 404     /404
ErrorDocument 500     /500

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteRule ^(resources)($|/) - [L]
RewriteRule ^user/([^/]*)$ /users.php?user=$1 [L]
RewriteRule ^dev/([^/]*)$ /dev.php?type=$1 [L]
RewriteRule ^users/([^/]*)$ /users.php?view=$1 [L]
RewriteRule ^users/edit/([^/]*)$ /users.php?view=edit&id=$1 [L]
RewriteRule ^settings/([^/]*)$ /settings.php?view=$1 [L]
RewriteRule ^groups/([^/]*)$ /groups.php?view=$1 [L]
RewriteRule ^groups/edit/([^/]*)$ /groups.php?view=edit&id=$1 [L]
RewriteRule ^groups/permissions/([^/]*)$ /groups.php?view=permissions.php&id=$1 [L]
RewriteRule ^page/([^/]*)$ /page.php?view=$1 [L]

This worked for me on domain.com but not anymore on domain.com/subdir Not only that, but isn't there a better way to process the get requests rewrite so I don't have to add a rule for every php file that does something with GET requests?

Thank you in advance.

anubhava
  • 761,203
  • 64
  • 569
  • 643
GRX
  • 479
  • 1
  • 5
  • 22
  • Possible duplicate of [.htaccess rewrite GET variables](http://stackoverflow.com/questions/7677070/htaccess-rewrite-get-variables) – Nishant Kumar Sep 21 '16 at 09:34
  • You can rewrite everything to `index.php` and add additional variables for the controllers and actions you want to use. – jeroen Sep 21 '16 at 09:35
  • Thank you @Bert, But this didn't work for me. Jeroen thanks for thinking with me, I'll draw out a scheme on how this would be achievable – GRX Sep 21 '16 at 09:40
  • @dream_machine that question is about *introducing* GET variables after a rewrite. This question, as far as I understand, is about *preserving* get variables. – Martin Nyolt Sep 21 '16 at 10:28
  • Your rules, of course, only apply to URLs that start with user/, dev/ and so on. How did you extend that to sub directories? I assume you would want to support URLs of the form `domain.com/subdir/user/…`. Probably extending the rule which *captures* the sub directory would be sufficient. – Martin Nyolt Sep 21 '16 at 10:32

3 Answers3

1

Try it like this,

RewriteEngine on

#if you want it to use for sub dir
RewriteBase /subdir/

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

#below for url which only have view as parameter
RewriteRule ^([\w]+)/([\w]+)$ $1.php?view=$2 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^user/([^/]*)$ users.php?user=$1
RewriteRule ^dev/([^/]*)$ dev.php?type=$1 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^(.*)/edit/([^/]*)$ /$1.php?view=edit&id=$2 [QSA,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^groups/permissions/([^/]*)$ /groups.php?view=permissions.php&id=$1 [QSA,L]
Abhishek Gurjar
  • 7,426
  • 10
  • 37
  • 45
  • Thank you for your response, This also isn't working for me sadly. – GRX Sep 21 '16 at 10:30
  • what is the exact problem please explain? which url is not working. – Abhishek Gurjar Sep 21 '16 at 10:32
  • I've added `RewriteRule ^settings/([^/]*)$ /settings.php?view=$1 [L]` but settings.php tells me the is no $_GET['view'] requested. that there's no $_GET at all. – GRX Sep 21 '16 at 10:37
1

You need to turn off MultiViews option and you can also merge some of your similar rules into one:

ErrorDocument 404     /404
ErrorDocument 500     /500
Options +FollowSymLinks -MultiViews

RewriteEngine On

RewriteRule ^(resources)($|/) - [L]

RewriteRule ^user/([^/]+)/?$ /users.php?user=$1 [L]

RewriteRule ^dev/([^/]+)/?$ /dev.php?type=$1 [L]

RewriteRule ^(users|settings|groups|page)/([^/]+)/?$ /$1.php?view=$2 [L,QSA,NC]

RewriteRule ^(users|groups)/(\w+)/([^/]+)/?$ /$1.php?view=$2&id=$3 [L,QSA,NC]

# To internally redirect /dir/file to /dir/file.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • 1
    I had to add Options +FollowSymLinks -MultiViews in the .htaccess of the parent dir, that. in combination with this one worked for me. thank you! – GRX Sep 21 '16 at 14:20
0

maybe you should copy this file into domain.com/subdir, but this is not that good, you should think of using a php mvc framework like symfony, it will resolve your problem smoothly, plain php is hard to extend.

  • It is in `domain.com/subdir`. And changing framework doesn't seem a viable option for a small issue like this... – GRX Sep 21 '16 at 10:30