Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 带两个参数的虚荣/清洁URL_Php_.htaccess_Url_Mod Rewrite_Url Rewriting - Fatal编程技术网

Php 带两个参数的虚荣/清洁URL

Php 带两个参数的虚荣/清洁URL,php,.htaccess,url,mod-rewrite,url-rewriting,Php,.htaccess,Url,Mod Rewrite,Url Rewriting,所以我有一个.htaccess文件,将/username放入profile.php?id=$0 我需要能够在不与从php文件中去掉.php扩展名的重写规则冲突的情况下接受两个参数 这里有一个我想要的明确例子: /profile.php?id=username&category=schooling-->/username/schooling 我不希望这会影响其他php文件,例如/about 我的.htaccess文件如下所示: Options +FollowSymlinks RewriteEngi

所以我有一个.htaccess文件,将/username放入profile.php?id=$0

我需要能够在不与从php文件中去掉.php扩展名的重写规则冲突的情况下接受两个参数

这里有一个我想要的明确例子:

/profile.php?id=username&category=schooling-->/username/schooling

我不希望这会影响其他php文件,例如/about

我的.htaccess文件如下所示:

Options +FollowSymlinks
RewriteEngine on
RewriteRule ^([A-Za-z0-9]+)$ $1.php
RewriteRule ^([A-Za-z0-9\._\-]+)+[^\.php]$ profile.php?id=$0 [NC]
任何帮助都将不胜感激,谢谢!:)


更新:

这对我来说很有效,但是图像不起作用

Options +FollowSymLinks -MultiViews
RewriteEngine On

RewriteRule ^([A-Za-z0-9]+)$ $1.php [L]
RewriteRule ^([A-Za-z0-9\._\-]+)+[^\.php]$ profile.php?id=$0 [L]
RewriteRule ^([^/]*)/([^/]*)$ /profile.php?id=$1&category=$2 [L]
有什么想法吗?我知道背后的原因,但不知道如何修复它。我想图像URL也被重定向了。

试试这个:

Options +FollowSymLinks -MultiViews
RewriteEngine On

# IF we have an existing file or folder
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
# THEN leave the url unchanged, and skip the next two rules
RewriteRule ^ - [L,S=2]
# ELSE (i.e., it's a pretty url that doesn't correspond to a real file or folder)
# Apply these rules if they match
RewriteRule ^(\w+)/?$ profile.php?id=$1 [L,QSA]
RewriteRule ^(\w+)/(\w+)/?$ profile.php?id=$1&category=$2 [L,QSA]
或许

Options +FollowSymlinks
RewriteEngine on
RewriteRule ^/([A-Za-z0-9\._\-]+)/([A-Za-z0-9]+)$ profile.php?id=$1&category=$2 [L, QSA, NC]
RewriteRule ^([A-Za-z0-9\._\-]+)+[^\.php]$ profile.php?id=$1 [L, QSA, NC]
RewriteRule ^([A-Za-z0-9]+)$ $1.php [L, QSA]

如果与
L
匹配的规则,则
L
标志用于停止mod_rewrite form处理剩余规则。

我在每个页面上都会收到404错误,包括基本php文件,例如/about它与我在子域上工作有什么关系吗?只是想知道,我认为这不会有影响。嗯,
.htaccess
必须在正确的文件夹中。假设文件位于
somefolder
中。url
/somefolder/a/b
将重定向到
somefolder/profile.php?etc
该文件存在吗?如果没有,我们将得到一个404。因此,我们要么修复路径,要么将htaccess放在正确的位置。我用基本的重写测试过它,所以我知道它是有效的。非常奇怪你能暂时把规则中的
L
改成
R
吗?这样,您将能够在浏览器栏中看到我们被重定向到的位置,以及该文件不存在的原因。谢谢。这会在每个页面上给我一个内部服务器错误,包括未重写的页面。对不起,我编辑了我的原始问题,并提供了对我有效的更新。在更新过程中,一切都正常,即使没有意义,但网站上的图像没有显示。我认为它们被视为profile.php重定向@zx81