Wednesday, September 21, 2022

[FIXED] How can i set htaccess file for virtual host?

Issue

I created a virtual host with this code :

<VirtualHost *:80>

    ServerAdmin admin@127.0.0.1
    ServerName site.ws
    ServerAlias www.site.ws
    DocumentRoot /home/me/Projects/website/build
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

<Directory /home/me/Projects/website/build>

        Allow from all
        Satisfy any

</Directory>

</VirtualHost>

and I created a .htaccess file in my /build directory with this code :

RewriteEngine On

RewriteRule   ^(.*)$    $1.html    [R,NC]

Consider my mod_rewrite is active in apache2, but I can't open pages with /filename

e.g site.ws/about

It shows error : The requested URL /about was not found on this server.


Solution

I try this with Apache2 2.4.27 in win:

First enable vhost in httpd.conf file.

vhost:

<VirtualHost *:80>
    ServerName site.ws
    DocumentRoot /home/me/Projects/website/build
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
    <Directory /home/me/Projects/website/build>
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>

.htaccess:

###START MOD_REWRITE
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /

    #REMOVE .html EXTENSION
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^([^\.]+)$ $1.html [NC,L]

</IfModule>
###END MOD_REWRITE


Answered By - benny-ben
Answer Checked By - Dawn Plyler (PHPFixing Volunteer)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.