.htaccess URL正在运行,但页面无法加载

时间:2014-01-14 20:01:00

标签: php regex apache .htaccess mod-rewrite

我正在尝试为我的网站网址创建.htaccess脚本,但该网页无法加载... Not Found The requested URL /Site/users/yax was not found on this server.

我希望<a href = 'http://localhost/Site/users/user.php?user=username'>View User</a>出现 像这样:点击http://localhost/Site/users/name但页面无法加载。

Apache代码:

RewriteEngine  on
RewriteBase /Site/users/

RewriteCond %{THE_REQUEST} /users\.php\?user=([^\s&]+) [NC]
RewriteRule ^ %1? [R=302,L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([a-zA-Z_]+)$ users.php?user=$1 [L] 

提前谢谢。

3 个答案:

答案 0 :(得分:1)

将规则更改为:

RewriteEngine  on
RewriteBase /Site/users/

RewriteCond %{THE_REQUEST} /users\.php\?user=([^\s&]+) [NC]
RewriteRule ^ %1? [R=302,L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^users/([a-zA-Z_]+)$ users.php?user=$1 [L] 

如果htaccess文件位于/Site/文件夹中,则“users”将成为重写规则与匹配的URI的一部分,无论您将重写基础设置为

另外,您应该将链接更改为http://localhost/Site/users/name,而不是每次都添加额外的往返重定向。

答案 1 :(得分:1)

试试这段代码:

Options +FollowSymLinks -MultiViews
RewriteEngine  on
RewriteBase /Site/

RewriteCond %{THE_REQUEST} /(users)/user\.php\?user=([^\s&]+) [NC]
RewriteRule ^ %1/%2? [R=302,L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(users)/(\w+)/?$ $1/user.php?user=$2 [L,QSA] 

答案 2 :(得分:0)

假设/Site/users/user.php是真实文件,并且在其URI中接受user = name

您的HTML:

<a href="http://localhost/Site/users/name">View User</a>

/ htaccess的:

RewriteEngine on
RewriteRule  ^Site/users/(.*)$  /Site/users/user.php?user=$1