Shahadat Hossain Khan Shahadat Hossain Khan - 1 month ago 20
PHP Question

.htaccess rewriterule path not found while accessing through ip address

I'm trying to set rewrite rule for my website through .htaccess. It works fine when access by domain name like -

http://www.example.com/admin/my/virtual/path

Problem is when I try to access through IP address, it returns 404 page.

I bind IP with my virtual path cause when I visit http://192.168.1.2/ it shows http://www.example.com/admin/ page i.e. home page of admin without any issue.

Following link doesn't work and returned 404 page -

http://192.168.1.2/my/virtual/path

I guess its .htaccess issue. Here is my .htaccess code FYI

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# The following rule tells Apache that if the requested filename
# exists, simply serve it.
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ index.php [L]
# The following rewrites all other queries to index.php. The
# condition ensures that if you are using Apache aliases to do
# mass virtual hosting, the base path will be prepended to
# allow proper resolution of the index.php file; it will work
# in non-aliased environments as well, providing a safe, one-size
# fits all solution.
RewriteCond %{REQUEST_URI}::$1 ^(/.+)(.+)::\2$
RewriteRule ^(.*) - [E=BASE:%1]
RewriteRule ^([^?]*)$ %{ENV:BASE}index.php [NC,L]


Here is my vhost configuration for IP -

<VirtualHost xxx.xxx.xxx.xxx:80>
DocumentRoot /path/of/my/hosting/location/public_html/admin
<Directory "/path/of/my/hosting/location/public_html/admin">
allow from all
Options None
Require all granted
</Directory>
</VirtualHost>


Here is my domain hosting part -

<VirtualHost example.com:80>
DocumentRoot /path/of/my/hosting/location/public_html
<Directory "/path/of/my/hosting/location/public_html">
allow from all
Options +FollowSymLinks
allowoverride all
Require all granted
HostNameLookups on
</Directory>
ServerName example.com
ServerAlias www.example.com
</VirtualHost>

Answer

The vhost part of the domain has allowoverride all inside, which tells apache to allow changes made .htaccess, however the vhost part of the ip don't have it.

Add the allowoverride all to the ip vhost-block and it should be ok.

<VirtualHost xxx.xxx.xxx.xxx:80>
     DocumentRoot /path/of/my/hosting/location/public_html/admin
     <Directory "/path/of/my/hosting/location/public_html/admin">
         allowoverride all
         allow from all
         Options None
         Require all granted
     </Directory>
</VirtualHost>