Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.htaccess 如何启用url重写?_.htaccess_Mod Rewrite_Apache2_Debian - Fatal编程技术网

.htaccess 如何启用url重写?

.htaccess 如何启用url重写?,.htaccess,mod-rewrite,apache2,debian,.htaccess,Mod Rewrite,Apache2,Debian,我在用Debian Jessie。我想启用URL重写。许多网站,如,建议我编辑 /etc/apache2/sites-available/default 但是,没有这样的文件,但它包含000-default.conf,其中包含以下代码: <VirtualHost *:80> ServerAdmin webmaster@localhost DocumentRoot /var/www/html ErrorLog ${APACHE_LOG_DIR}/error.log CustomL

我在用Debian Jessie。我想启用URL重写。许多网站,如,建议我编辑

/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>
我可以用上面指定的博客文章中给出的代码覆盖这个文件吗

在那之后,我可以用下面的代码在我的/var/www/html/somesite/文件夹中放置一个.htaccess文件吗

RewriteEngine On
RewriteRule  ^this/page/thispage this.php?page=thispage
要用“this/page/thispage/”覆盖“this.php?page=thispage”吗

还有(附加问题),是否有方法覆盖所有GET请求,以便“this.php?variable1=foo&variable2=bar”形式的任何链接都将生成一个“this/variable1/foo/variable2/bar”形式?

您需要添加:

<Directory "/var/www/html">
    Allow from all
    AllowOverride All
</Directory>

当您在浏览器中输入此URL时,就会出现这种情况:
http://example.com/this/a/b/c/d
您将看到页面
/this.php?a=b&c=d
,URL不会更改。

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

a2enmod重写

如果未装入,请执行以下操作:

apt get install rewrite

然后使用以下命令编辑vhost区域:

Allow from all
允许全部覆盖

如果尚未创建vhost文件,则可以从默认值复制它:

cd/etc/apache2/可用站点

cp 000 default yourwebsite.com

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

a2ensite yourwebsite.com


最后,您需要将
.htaccess
文件放在文档根目录中

谢谢,还有一个问题。我的目标是什么?我应该设置一个新的虚拟主机,还是可以使用000-default.conf。(请注意,我的文档根目录上有多个项目)@Yatoom“vhost”=“虚拟主机”。视情况而定,“默认”是当请求与任何其他vhost的
ServerName
ServerAlias
不匹配时将使用的虚拟主机。如果您计划托管许多站点,您可能希望为每个站点设置特定的vhost,但是如果您只有一个站点,那么使用默认值就可以了。
<Directory "/var/www/html">
    Allow from all
    AllowOverride All
</Directory>
Options -Multiviews
RewriteEngine On
RewriteRule  ^this/([^/]+)/([^/]+)$ this.php?$1=$2 [L]
RewriteRule  ^this/([^/]+)/([^/]+)/([^/]+)/([^/]+)$ this.php?$1=$2&$3=$4 [L]