简体   繁体   English

.htaccess链接有效,但没有GET变量

[英].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. 在我的一个网站上,.htaccess可用于链接,但php文件未检测到GET方法。

My .htaccess 我的.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? 这在domain.com上对我有效,但在domain.com/subdir上不再有效,不仅如此,而且没有更好的方法来处理get请求的重写,因此我不必为每个php文件添加规则与GET请求有关?

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: 您需要关闭MultiViews选项,还可以将一些类似的规则合并为一个:

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很难扩展。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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