Thomas
Thomas

Reputation: 701

.htaccess mod_rewrites appends document root to URL

In essence possibly quite similar to that question: Document root appending in url rewriting

The possible solutions linked in that question did not work.

Problem: I want to use .htaccess mod_rewrite in order to:

My /etc/apache2/apache2.conf:

<Directory /var/www/>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
</Directory>

My /etc/apache2/sites-available/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>

My /var/www/html/.htaccess:

RewriteEngine On
<lots of misguided attempts went there>

Thanks for your help.

Upvotes: 0

Views: 80

Answers (1)

Thomas
Thomas

Reputation: 701

Thanks @Bijay Regmi, I managed to reach the behavior I wanted. It took a combination of things to get it right:

# Removing the indexes
Options -Indexes

# Removing the auto-slashing for directories
DirectorySlash off

RewriteEngine on
RewriteOptions AllowNoSlash

# Show file instead of folder if file exist
# URL: /docs/tutorial
# SERVE: /docs/tutorials.html
# FINAL_URL: /docs/tutorial
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*) $1.html [NC,L]

# Prettify URLs
# URL: /docs/tutorial/tuto
# SERVE: /docs/tutorials/tuto.html
# FINAL_URL: /docs/tutorial/tuto
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*) $1.html [NC,L]

# Folder contain another sub-site
# URL: /docs/api
# SERVE: /docs/api/index.html
# FINAL_URL: /docs/api/index.html
RewriteCond %{REQUEST_URI} \/docs\/api$
RewriteRule ^(.*) %{REQUEST_URI}/index.html [R,NC,L]

# Show index file of folder when folder requested
# URL /get_started
# SERVE: /get_started/index.html
# FINAL_URL /get_started
RewriteCond %{REQUEST_URI} !\/$
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_FILENAME}/index.html -f
RewriteRule ^(.*) $1/index.html [NC,L]

Upvotes: 2

Related Questions