2

Here is my localhost folder structure:

•www (wamphost folder)

    •example.com (folder)

        •index.php

With the current the URL results:

localhost/example.com/

The URL I would like is:

localhost/example.com

What I would like:

My intention is remove the slash beside URL, but I tried so many ways and still no clue how. I am new to rewrites and no matter how I change the URL keep adding the slash by itself.

My .htaccess file:

Options +FollowSymLinks -MultiViews -Indexes
RewriteEngine On

DirectoryIndex index.php
DirectorySlash Off    

RewriteRule ^example.com$ /domain.com/index.php [L,E=LOOP:1]

RewriteCond %{ENV:REDIRECT_LOOP} !1
RewriteRule ^example.com/$ /domain.com [R=301,L]

RewriteCond %{ENV:REDIRECT_LOOP} !1
RewriteRule ^example.com/index.php$ /domain.com [R=301,L]
1

Since you already have the DirectorySlash Off directive, what you are probably seeing is a cached redirect.

Normally, mod_dir (DirectorySlash On) will append a slash when requesting a real directory (this is required to make the request valid) by issuing a 301 redirect to the slashed URL. 301 (permanent) redirects are naturally cached by the browser.

In order to still serve the DirectoryIndex then you need to now do this manually yourself and internally rewrite to the slashed URL, or explicitly to the index document (eg. index.php) if this is known. Otherwise you'll likely get a 403 Forbidden (unless mod_autoindex is configured to generate a directory listing).

So, something like the following:

DirectorySlash Off

RewriteCond %{REQUEST_FILENAME} -d
RewriteRule !/$ %{REQUEST_URI}/ [L]

The mod_rewrite directives state... for all URLs that don't end in a slash that do map to physical directories then internally rewrite and append a slash. This will then allow mod_dir to further rewrite the request and serve the DirectoryIndex (ie. index.php).

I assume all your internal links in the HTML do not include the trailing slash.

You might, however, need to issue a 301 redirect if the slashed URL is accessed directory - but you only really need to do this if these URLs have already been indexed by the search engines or are being linked to.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.