简体   繁体   中英

.htaccess links working, but no GET variables

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.

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]

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]

也许您应该将此文件复制到domain.com/subdir ,但这不是那么好,您应该考虑使用像symfony这样的php mvc框架,它将顺利解决您的问题,纯PHP很难扩展。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM