【问题标题】:Apache php, mod_rewrite seems not to work properlyApache php,mod_rewrite 似乎无法正常工作
【发布时间】:2015-12-03 00:30:45
【问题描述】:

我正在使用 Apache 服务器中的 PHP 应用程序。我想使用 rewrite php 模块来转换所有 url 链接,如下例所示:www.mysite.com/home.php ---> www.mysite.com/home

问题是模块只为第一个规则(home-->home.php)加载和运行,所有其他规则都被忽略了。

我不明白问题出在哪里

这是我的 .htaccess 文件

<IfModule mod_rewrite.c>
Options +FollowSymlinks
# Options +SymLinksIfOwnerMatch
Options +FollowSymlinks
RewriteEngine On
# RewriteBase /
</IfModule>



# Option 1:
# Rewrite "www.example.com -> example.com".
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]
</IfModule>



RewriteRule      ^home$                          index.php                    [NC,L]
RewriteRule      ^ga$                    ga.php                         [NC,L]
RewriteRule          ^info                       info.php                                       [NC,L]
RewriteRule      ^ecosoc                     ecosoc.php                   [NC,L]
RewriteRule      ^sc                         sc.php                       [NC,L]
RewriteRule      ^icj                        icj.php                      [NC,L]
#RewriteRule     ^gallery$                  gallery.php                  [NC,L]
#RewriteRule     ^archive$                  archive/index.php            [NC,L]
RewriteRule      ^contact$                   contact.php                  [NC,L]


#RewriteRule      ^login$                    login.php                    [NC,L]


RewriteRule      ^registration$             registration.php                  [NC,L]
RewriteRule      ^delegate$                 delegate.php                      [NC,L]
RewriteRule      ^delegate/success$         delegate.php?result=success       [NC,L]
RewriteRule      ^delegate/error$           delegate.php?result=error         [NC,L]

RewriteRule      ^iscrizioni/confirm/([a-z]*)$    confirm_registration.php?action=$1  [NC,L]
RewriteRule      ^iscrizioni/confirm/user/(.*)$   confirm_registration.php?action=confirm&code=$1  [NC,L]



#RewriteRule      ^course$                   course.php?e=error               [NC,L]
RewriteRule      ^staff$                    staff.php                        [NC,L]
RewriteRule      ^staff/success$            staff.php?result=success         [NC,L]

#RewriteRule      ^schoolarea/$              http://school.rimun.com          [NC,L]

这是我的 vhost .conf 文件

#
# public-website
#

<Directory  /var/www/public-website>
    Options Indexes FollowSymLinks MultiViews
    AllowOverride all

    Require all granted
</Directory>

<VirtualHost *:80>
    DirectoryIndex index.php  #settare al nome file da aprire all'accesso al sito
    ServerName sociall.local
    ServerAdmin croce.1546488@studenti.uniroma1.it
    DocumentRoot /var/www/public-website/

    ErrorLog /var/www/public-website/error.log
    #CustomLog /var/www/html/sociall/log/access.log combined
</VirtualHost>

感谢您的支持。

【问题讨论】:

  • 如果删除表格的第一行,即 home 行,第二行是否有效?
  • 我的问题不同,对我来说,重写模块部分工作

标签: php apache .htaccess mod-rewrite virtualhost


【解决方案1】:

你需要清理你的 htaccess 文件。

创建您的 htaccess 文件的副本。

删除原始文件中的所有内容。

将此添加为首发

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
Options +FollowSymlinks

RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]

RewriteRule ^home$ index.php [NC,L]
RewriteRule ^ga$ ga.php [NC,L]
</IfModule>

现在重定向到家并且 ga 应该可以工作了。如果它们不起作用,您将问题本地化到此代码。尝试修复它。当您修复此代码时,您可以一一附加其余的重写规则(不是一次全部)。

【讨论】:

  • 是否可以在此处发布您网站的网址?您是否尝试过我发布的确切代码?
  • 我已经完全按照你给我的方式做了。我想准确说明该模块在 Web 服务器上正常工作。它不在我的本地主机服务器上。顺便说一句,该网站是 rimun.com。它也可以在带有 MAMP 的 Macbook 上正常工作。它仅不适用于我的带有 Apache 服务器的 linux PC。
猜你喜欢
  • 1970-01-01
  • 2015-04-06
  • 1970-01-01
  • 2017-09-04
  • 2012-05-06
  • 2013-02-17
  • 2017-10-09
  • 2017-08-03
  • 2014-05-02
相关资源
最近更新 更多