大网数据广告活动
网站运营与故障问答

Url rewrite not work in wamp localhost

· 52CE 我爱测

I am developing a PHP web application. I have pages, posts, categories, product and etc. Here is my .htaccess file to create SEO friendly URL: Options -Indexes RewriteEngine on RewriteRule ^Page/(.*)/(.*)$ Page.php?Page_Id=$1&Pag…

问题原文

原帖链接 · Mohammadreza Esmaeeli · CC BY-SA 3.0

I am developing a PHP web application. I have pages, posts, categories, product and etc.

Here is my .htaccess file to create SEO friendly URL:

Options -Indexes
RewriteEngine on
RewriteRule ^Page/(.*)/(.*)$ Page.php?Page_Id=$1&Page_Title=$2 [NC,L]
RewriteRule ^Product/(.*)/(.*)$ Product.php?Product_Id=$1&Product_Title=$2 [NC,L]
RewriteRule ^Category/(.*)/(.*)$ Category.php?Category_Id=$1&Category_Title=$2 [NC,L]
RewriteRule ^Single/(.*)/(.*)$ Single.php?Post_Id=$1&Post_Title=$2 [NC,L]
RewriteRule ^Blog/(.*)/(.*)$ Blog.php?Category_Id=$1&Category_Title=$2 [NC,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)$ $1.php [L,QSA]


<FilesMatch "\.(htaccess|htpasswd|ini|phps|fla|psd|log|sh)$">
    Order Allow,Deny
    Deny from all
</FilesMatch>
<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresDefault "access plus 1 month"
    ExpiresByType image/x-icon "access plus 1 year"
    ExpiresByType image/gif "access plus 1 month"
    ExpiresByType image/png "access plus 1 month"
    ExpiresByType image/jpg "access plus 1 month"
    ExpiresByType image/jpeg "access plus 1 month"
    ExpiresByType text/css "access plus 1 month"
    ExpiresByType application/javascript "access plus 1 year"
</IfModule>

When I visit Page.php?Page_Id=13 it works. And also when I visit Page/13/Page-Title it works. Everything is good when I test my application on the server, but it doesn't work in wamp localhost.

I active

LoadModule rewrite_module modules/mod_rewrite.so

in Apache http.conf and here is my vhost:

    <VirtualHost *:8080>
    ServerName localhost
    DocumentRoot c:/wamp64/www
    <Directory  "c:/wamp64/www/">
        Options +Indexes +Includes +FollowSymLinks +MultiViews
        AllowOverride All
        Require local
    </Directory>
</VirtualHost>


<VirtualHost *:8080>
    ServerName schoolarshop
    DocumentRoot "c:/wamp64/www/schoolarshop"
    <Directory  "c:/wamp64/www/schoolarshop/">
        Options +Indexes +Includes +FollowSymLinks +MultiViews
        AllowOverride none
        Require local
    </Directory>
</VirtualHost>

Example of error in localhost is:

Notice: Undefined index: Page_Id in Page.php on line 8

Why does it work on the server and not in localhost?

I Googled a lot but it can't find anything.

已采纳解答

原帖链接 · MrWhite · CC BY-SA 3.0

Options +Indexes +Includes +FollowSymLinks +MultiViews

Why are you explicitly enabling MultiViews in your server config? This is likely the cause of your problem. Try removing +MultiViews from your server config (above), and/or disable MultiViews at the top of your .htaccess:

 Options -Indexes -MultiViews

MutliViews is disabled on a default Apache install, however, some (shared) hosts do seem to enable it. So, disabling it in .htaccess as a matter of course is probably recommended.

(But also, why are you enabling Includes - is this really necessary? Since you are using PHP, this is unlikely.)

The effect of MultiViews is noticeable because you have effectively removed the file extension to create your user-friendly URLs.

With MultiViews enabled (part of mod_negotiation)... when you request Page/13/Page-Title, Apache tries to find a matching file in the current/root directory that matches the basename Page, by trying various extensions that would return the appropriate mime-type. This occurs before mod_rewrite is able to rewrite your friendly URL. So, MultiViews ends up rewriting your request to Page.php without passing any of the URL params (the rest of the URL will likely be seen as PATH_INFO), hence your "Undefined index" PHP notice.

(Although you should also be checking for the existence of this $_GET variable in your PHP script, which you don't seem to be doing.)

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Webmasters Stack Exchange
适用范围
以原帖系统、软件版本与发布日期为准(2017-07-03)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。