如何使apache中的重写模块工作?

时间:2018-10-01 12:21:52

标签: apache lamp

apache的重写mod在vps服务器LAMP(ubuntu 18.04)上不起作用

我做了什么:

1)安装apache2,php-7.2和mysql,然后为apache配置虚拟主机(创建文件:/etc/sites-available/example.com.conf)-其中example.com是我的域

<VirtualHost *:80>
  ServerAdmin admin@example.com
  ServerName example.com
  ServerAlias www.example.com
  DocumentRoot /var/www/example.com/html
  ErrorLog ${APACHE_LOG_DIR}/error.log
  CustomLog ${APACHE_LOG_DIR}/access.log combined   
</VirtualHost>

2)启用当前虚拟主机:

sudo a2ensite example.com.conf

3)禁用默认值:

sudo a2dissite 000-default.conf

4)激活Mod_Rewrites

sudo a2enmod rewrite

5)在/etc/apache2/apache2.conf中添加配置

<Directory /var/www/example.com/html/>
    Options Indexes FollowSymLinks MultiViews
    AllowOverride All
    Order allow,deny
    allow from all
</Directory>

6)重新启动服务器

sudo systemctl restart apache2

主页正常工作,但是如果我单击example.com/product-page-1的链接,则会显示:

The requested URL /product-page-1 was not found on this server.

我做错了什么?

更新

.htaccess文件

<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
    Options -MultiViews -Indexes
</IfModule>

RewriteEngine On

# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]

# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

# some redirects
Redirect 301 /create-account.html /register
...

</IfModule>

更新2

我在htaccess中有25-30 301个重定向,如果不删除它们,服务器将失败

The server encountered an internal error or misconfiguration and was unable to complete your request.

如果我在appache2的错误日志中查看,我会看到:

/var/www/example.com/html/.htaccess: Invalid command 'Register', perhaps misspelled or defined by a module not included in the server configuration

哪些模块需要301重定向才能正常工作?

0 个答案:

没有答案