Apache网站从别名转换为VirtualHost

发布于 2025-02-04 03:48:30 字数 6188 浏览 4 评论 0原文

我必须接管由不再在那里的同事配置的内部Web服务器。我们的开发人员团队要求我将应用程序URL从别名转换为a virtualhosts (子域名命名)系统。例如:

https://srv-intra.mydomain.fr/basecolldev

https://srv-intra.mydomain.fr/basecolldev

https://basecolldev.mydomain.fr

环境规格:

  • Linux Opensuse opensuse opensues opensuse leap 15.3
  • 服务器版本:apache/2.4.51/2.4.51 (Linux/Suse)
  • PHP 7.4.6(CLI)(NTS)

问题描述:我可以连接到https://basecolldev.mydomain.fr,尽管显示了一些404个错误在Access_log文件中(请参阅帖子末尾)。然后,当单击将重定向到https://basecolldev.mydomain.fr/login的登录按钮时,我有一个404错误页面。当我手动将URL修改为https://basecolldev.mydomain.fr/index.php/login时,找到并正确显示了登录页面。这意味着Home Page连接不需要/Index.php,而是用于浏览网站。我需要永久防止/index.php后缀以显示在URL中。

我在下面描述了我到目前为止配置的内容以及仍然没有功能的内容。

我首先创建了一个新的vhost文件/etc/apache2/vhost.d/basecolldev-ssl.conf

<VirtualHost basecolldev.mydomain.fr:443>

    DocumentRoot "/var/www/BaseCollDev/public"
    ServerName basecolldev.mydomain.fr
    ErrorLog /var/log/apache2/basecolldev-error_log
    TransferLog /var/log/apache2/basecolldev-access_log
    LogLevel alert rewrite:trace8

    <Directory /var/www/BaseCollDev/public>
         #Order allow,deny
         #allow from all
         #AllowOverride All
         Require all granted
         Options -Indexes -Includes -ExecCGI -FollowSymlinks
    </Directory>

</VirtualHost>

然后,我评论了与应用程序Directory Directory tag在文件/etc/apache2/default-server.conf

DocumentRoot "/var/www"

<Directory "/var/www">
    Options FollowSymLinks
    AllowOverride None
    <IfModule !mod_access_compat.c>
            Require all granted
    </IfModule>
    <IfModule mod_access_compat.c>
        Order allow,deny
        Allow from all
    </IfModule>
</Directory>

Alias /icons/ "/usr/share/apache2/icons/"
Alias /phpmyadmin "/srv/www/htdocs/phpMyAdmin"

<Directory "/usr/share/apache2/icons">
    Options Indexes MultiViews
    AllowOverride None
    <IfModule !mod_access_compat.c>
        Require all granted
    </IfModule>
    <IfModule mod_access_compat.c>
        Order allow,deny
        Allow from all
    </IfModule>
</Directory>


#<Directory "/var/www/BaseCollDev/public">
#        AllowOverride All
#        Require all granted
#</Directory>

ScriptAlias /cgi-bin/ "/srv/www/cgi-bin/"

<Directory "/srv/www/cgi-bin">
    AllowOverride None
    Options +ExecCGI -Includes
    <IfModule !mod_access_compat.c>
        Require all granted
    </IfModule>
    <IfModule mod_access_compat.c>
        Order allow,deny
        Allow from all
    </IfModule>
</Directory>

<IfModule mod_userdir.c>
    UserDir public_html
    Include /etc/apache2/mod_userdir.conf
</IfModule>


IncludeOptional /etc/apache2/conf.d/*.conf

IncludeOptional /etc/apache2/conf.d/apache2-manual?conf

/var/var/var/log/log/apache2/basecolldev-access_log文件>与主页连接有关的文件:

10.9.4.140 - - [03/Jun/2022:11:02:31 +0200] "GET / HTTP/1.1" 200 54206
10.9.4.140 - - [03/Jun/2022:11:02:31 +0200] "GET /build/runtime.d94b3b43.js HTTP/1.1" 200 1505
10.9.4.140 - - [03/Jun/2022:11:02:31 +0200] "GET /build/app.13d64c6c.js HTTP/1.1" 200 236
10.9.4.140 - - [03/Jun/2022:11:02:31 +0200] "GET /build/2.9935185b.css HTTP/1.1" 200 64639
10.9.4.140 - - [03/Jun/2022:11:02:31 +0200] "GET /build/app.6cefaab5.css HTTP/1.1" 200 163752
10.9.4.140 - - [03/Jun/2022:11:02:31 +0200] "GET /build/2.28b18d57.js HTTP/1.1" 200 261268
10.9.4.140 - - [03/Jun/2022:11:02:31 +0200] "GET /build/images/logoEdVBlanc.png HTTP/1.1" 200 6579
10.9.4.140 - - [03/Jun/2022:11:02:31 +0200] "GET /_wdt/b07606 HTTP/1.1" 404 1280
10.9.4.140 - - [03/Jun/2022:11:02:31 +0200] "GET /build/fonts/fa-solid-900.e8a427e1.woff2 HTTP/1.1" 200 78196
10.9.4.140 - - [03/Jun/2022:11:02:32 +0200] "GET /favicon.ico HTTP/1.1" 404 1280
10.9.4.140 - - [03/Jun/2022:11:02:32 +0200] "GET /_wdt/b07606 HTTP/1.1" 404 1280
10.9.4.140 - - [03/Jun/2022:11:02:33 +0200] "GET /_wdt/b07606 HTTP/1.1" 404 1280
10.9.4.140 - - [03/Jun/2022:11:02:34 +0200] "GET /_wdt/b07606 HTTP/1.1" 404 1280
10.9.4.140 - - [03/Jun/2022:11:02:35 +0200] "GET /_wdt/b07606 HTTP/1.1" 404 1280

/var/log/apache2/basecolldev-access_log与登录页面的连接有关的文件:

10.9.4.140 - - [03/Jun/2022:11:03:47 +0200] "GET /login HTTP/1.1" 404 1280

我在/var/var/var/log/log/apache2/basecolldev-error_log中看到没有logs 。


我附上.htaccess文件内容(如有必要)。

文件/var/www/.htaccess

# BEGIN WordPress
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
</IfModule>
# END WordPress

文件/var/www/basecolldev/public/.htaccess::

DirectoryIndex index.php

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

<IfModule mod_rewrite.c>
    RewriteEngine On

    RewriteCond %{REQUEST_URI}::$0 ^(/.+)/(.*)::\2$
    RewriteRule .* - [E=BASE:%1]

    RewriteCond %{HTTP:Authorization} .+
    RewriteRule ^ - [E=HTTP_AUTHORIZATION:%0]

    RewriteCond %{ENV:REDIRECT_STATUS} =""
    RewriteRule ^index\.php(?:/(.*)|$) %{ENV:BASE}/$1 [R=301,L]

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ %{ENV:BASE}/index.php [L]
 </IfModule>

 <IfModule !mod_rewrite.c>
     <IfModule mod_alias.c>
        RedirectMatch 307 ^/$ /index.php/
     </IfModule>
 </IfModule>

I have to take over an internal Web server that has been configured by a colleague who is no longer there. Our developpers team asks me to convert applications URLs from an Alias to a Virtualhosts (subdomain naming) system. For instance:

https://srv-intra.mydomain.fr/basecolldev

should become

https://basecolldev.mydomain.fr

Environment specifications:

  • Linux OpenSUSE Leap 15.3
  • Server version: Apache/2.4.51 (Linux/SUSE)
  • PHP 7.4.6 (cli) ( NTS )

Issue description: I can connect to https://basecolldev.mydomain.fr despite some 404 errors showing in access_log file (see at the end of the post). I have then a 404 error page when clicking the login button that redirects to https://basecolldev.mydomain.fr/login. The login page is found and displayed properly when I manually modify the URL to https://basecolldev.mydomain.fr/index.php/login. This means that /index.php is not required for home page connection but is for browsing the website. I need to permanently prevent the /index.php suffix to show up in URL.

I describe below what I have configured so far and what remains unfunctionnal.

I have first created a new vhost file /etc/apache2/vhost.d/basecolldev-ssl.conf :

<VirtualHost basecolldev.mydomain.fr:443>

    DocumentRoot "/var/www/BaseCollDev/public"
    ServerName basecolldev.mydomain.fr
    ErrorLog /var/log/apache2/basecolldev-error_log
    TransferLog /var/log/apache2/basecolldev-access_log
    LogLevel alert rewrite:trace8

    <Directory /var/www/BaseCollDev/public>
         #Order allow,deny
         #allow from all
         #AllowOverride All
         Require all granted
         Options -Indexes -Includes -ExecCGI -FollowSymlinks
    </Directory>

</VirtualHost>

I have then commented the lines related to the application Directory tag in file /etc/apache2/default-server.conf :

DocumentRoot "/var/www"

<Directory "/var/www">
    Options FollowSymLinks
    AllowOverride None
    <IfModule !mod_access_compat.c>
            Require all granted
    </IfModule>
    <IfModule mod_access_compat.c>
        Order allow,deny
        Allow from all
    </IfModule>
</Directory>

Alias /icons/ "/usr/share/apache2/icons/"
Alias /phpmyadmin "/srv/www/htdocs/phpMyAdmin"

<Directory "/usr/share/apache2/icons">
    Options Indexes MultiViews
    AllowOverride None
    <IfModule !mod_access_compat.c>
        Require all granted
    </IfModule>
    <IfModule mod_access_compat.c>
        Order allow,deny
        Allow from all
    </IfModule>
</Directory>


#<Directory "/var/www/BaseCollDev/public">
#        AllowOverride All
#        Require all granted
#</Directory>

ScriptAlias /cgi-bin/ "/srv/www/cgi-bin/"

<Directory "/srv/www/cgi-bin">
    AllowOverride None
    Options +ExecCGI -Includes
    <IfModule !mod_access_compat.c>
        Require all granted
    </IfModule>
    <IfModule mod_access_compat.c>
        Order allow,deny
        Allow from all
    </IfModule>
</Directory>

<IfModule mod_userdir.c>
    UserDir public_html
    Include /etc/apache2/mod_userdir.conf
</IfModule>


IncludeOptional /etc/apache2/conf.d/*.conf

IncludeOptional /etc/apache2/conf.d/apache2-manual?conf

Extract from /var/log/apache2/basecolldev-access_log file relating to a connection to the home page:

10.9.4.140 - - [03/Jun/2022:11:02:31 +0200] "GET / HTTP/1.1" 200 54206
10.9.4.140 - - [03/Jun/2022:11:02:31 +0200] "GET /build/runtime.d94b3b43.js HTTP/1.1" 200 1505
10.9.4.140 - - [03/Jun/2022:11:02:31 +0200] "GET /build/app.13d64c6c.js HTTP/1.1" 200 236
10.9.4.140 - - [03/Jun/2022:11:02:31 +0200] "GET /build/2.9935185b.css HTTP/1.1" 200 64639
10.9.4.140 - - [03/Jun/2022:11:02:31 +0200] "GET /build/app.6cefaab5.css HTTP/1.1" 200 163752
10.9.4.140 - - [03/Jun/2022:11:02:31 +0200] "GET /build/2.28b18d57.js HTTP/1.1" 200 261268
10.9.4.140 - - [03/Jun/2022:11:02:31 +0200] "GET /build/images/logoEdVBlanc.png HTTP/1.1" 200 6579
10.9.4.140 - - [03/Jun/2022:11:02:31 +0200] "GET /_wdt/b07606 HTTP/1.1" 404 1280
10.9.4.140 - - [03/Jun/2022:11:02:31 +0200] "GET /build/fonts/fa-solid-900.e8a427e1.woff2 HTTP/1.1" 200 78196
10.9.4.140 - - [03/Jun/2022:11:02:32 +0200] "GET /favicon.ico HTTP/1.1" 404 1280
10.9.4.140 - - [03/Jun/2022:11:02:32 +0200] "GET /_wdt/b07606 HTTP/1.1" 404 1280
10.9.4.140 - - [03/Jun/2022:11:02:33 +0200] "GET /_wdt/b07606 HTTP/1.1" 404 1280
10.9.4.140 - - [03/Jun/2022:11:02:34 +0200] "GET /_wdt/b07606 HTTP/1.1" 404 1280
10.9.4.140 - - [03/Jun/2022:11:02:35 +0200] "GET /_wdt/b07606 HTTP/1.1" 404 1280

Extract from /var/log/apache2/basecolldev-access_log file relating to a connection to the login page:

10.9.4.140 - - [03/Jun/2022:11:03:47 +0200] "GET /login HTTP/1.1" 404 1280

I see no logs in /var/log/apache2/basecolldev-error_log file.


I enclose .htaccess files contents if necessary.

File /var/www/.htaccess :

# BEGIN WordPress
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
</IfModule>
# END WordPress

File /var/www/BaseCollDev/public/.htaccess :

DirectoryIndex index.php

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

<IfModule mod_rewrite.c>
    RewriteEngine On

    RewriteCond %{REQUEST_URI}::$0 ^(/.+)/(.*)::\2$
    RewriteRule .* - [E=BASE:%1]

    RewriteCond %{HTTP:Authorization} .+
    RewriteRule ^ - [E=HTTP_AUTHORIZATION:%0]

    RewriteCond %{ENV:REDIRECT_STATUS} =""
    RewriteRule ^index\.php(?:/(.*)|$) %{ENV:BASE}/$1 [R=301,L]

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ %{ENV:BASE}/index.php [L]
 </IfModule>

 <IfModule !mod_rewrite.c>
     <IfModule mod_alias.c>
        RedirectMatch 307 ^/$ /index.php/
     </IfModule>
 </IfModule>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

恍梦境° 2025-02-11 03:48:30

您在&lt;目录“/var/www”&gt;中具有walloverride none。这意味着您的.htaccess文件,因此您的mod_rewrite配置没有读取。最简单的选项是allowoverride all(如果需要,也可以指定特定的覆盖)。

您还需要允许以下符号链接(删除-followSymlinks,可能会添加+collassymlinks)。

You have AllowOverride None in your <Directory "/var/www">. This means your .htaccess file and therefore your mod_rewrite configuration is not being read. The simplest option is AllowOverride All (you can specify specific overrides too if you want).

You will also need to allow following symlinks (remove -FollowSymlinks, possibly add +FollowSymlinks).

夜血缘 2025-02-11 03:48:30

我最终通过以下内容设法使其功能性,以下内容:

file /etc/apache2/default-server.conf&gt;更改允许覆盖all

<Directory "/var/www">
    ...
    AllowOverride All
    ...
</Directory>

文件/etc/apache2/vhosts.d/basecoll-ssl.conf&gt;删除-followsymlinks选项:

<VirtualHost basecolldev.mydomain.fr:443>
    ...
    <Directory /var/www/BaseCollDev/public>
        ... 
        Options -Indexes -Includes -ExecCGI
        ...
    </Directory>
</VirtualHost>

感谢您的帮助,使我处于正确的方式!

I eventually managed to make it functional, by following:

File /etc/apache2/default-server.conf > changed Allow Override to All:

<Directory "/var/www">
    ...
    AllowOverride All
    ...
</Directory>

File /etc/apache2/vhosts.d/basecoll-ssl.conf > removed -FollowSymlinks option:

<VirtualHost basecolldev.mydomain.fr:443>
    ...
    <Directory /var/www/BaseCollDev/public>
        ... 
        Options -Indexes -Includes -ExecCGI
        ...
    </Directory>
</VirtualHost>

Thanks for help that put me on the right way!

赏烟花じ飞满天 2025-02-11 03:48:30

使用AliasMatch代替别名:

AliasMatch ^/bar/?(.*)/var/www/bar/$ 1

Use AliasMatch instead of Alias:

AliasMatch ^/bar/?(.*) /var/www/bar/$1

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文