如何启用网址重写?

时间:2014-07-18 11:30:39

标签: .htaccess mod-rewrite apache2 debian

我正在使用Debian Jessie。我想启用URL重写。许多网站(例如this blog)都建议我编辑

/etc/apache2/sites-available/default

但是,没有这样的文件,但它包含000-default.conf,其中包含以下代码:

<VirtualHost *:80> 
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html

ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

没有包含

的行
AllowOverride None

我可以使用上面指定的博客文章中给出的代码覆盖此文件吗?

之后,我可以使用以下代码将.htaccess文件放入/ var / www / html / somesite /文件夹中

RewriteEngine On
RewriteRule  ^this/page/thispage this.php?page=thispage

用“this / page / thispage /”覆盖“this.php?page = thispage”?

和(奖金问题),是否有办法覆盖所有GET请求,因此“this.php?variable1 = foo&amp; variable2 = bar”形式的任何链接都将生成“this / variable1 / foo”形式/变量2 /栏“?

2 个答案:

答案 0 :(得分:1)

您需要添加:

<Directory "/var/www/html">
    Allow from all
    AllowOverride All
</Directory>
你的vhost中的

您的htaccess文件需要进入文档根目录,即/var/www/html

然后你可以这样做:

Options -Multiviews
RewriteEngine On
RewriteRule  ^this/([^/]+)/([^/]+)$ this.php?$1=$2 [L]
RewriteRule  ^this/([^/]+)/([^/]+)/([^/]+)/([^/]+)$ this.php?$1=$2&$3=$4 [L]

当您将此网址输入浏览器:http://example.com/this/a/b/c/d时,这将使成为,您将看到页面/this.php?a=b&c=d,并且网址不会更改。

答案 1 :(得分:1)

首先,您需要在计算机上启用重写模式:

a2enmod rewrite

如果它没有被确定,请执行此操作:

apt-get install rewrite

然后使用:

编辑您的虚拟区域

Allow from all AllowOverride All

如果您还没有创建vhost文件,可以将其从默认文件中复制出来:

cd /etc/apache2/sites-available

cp 000-default yourwebsite.com

为新网站创建vhost文件后,启用它:

a2ensite yourwebsite.com

最后,您需要在文档ROOT中放置.htaccess文件