Dhay
Dhay

Reputation: 621

mod_rewrite into subdirectory file from folder

This could be probably duplicate due to unable to find that original even after checked the similar questions list of stackoverflow. Actually if user type myipaddress/gt then it should redirect to myipaddress/gt/gt.htm I tried various mod_rewrites but still gets the 'gt' folder contents listed in browser. I gave the last try with below mod_rewrite.

Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteRule ^gt$ gt/gt.htm [NC,L]

But still the browser shows the contents of the 'gt' folder. What am I doing wrong?

Upvotes: 1

Views: 76

Answers (2)

Mike Rockétt
Mike Rockétt

Reputation: 9007

Due to the fact that Apache sees gt as a directory, it treats it as such. However, that doesn't stop all rewriting for that particular path.

This is the basic process:

  1. You navigate to /gt.
  2. Apache sees that as an existing directory, and thus redirects to /gt/.
  3. Your rule is ignored, because it doesn't contain the trailing slash.

This is why you still see the directory listing.

As such, you should change your rule to this:

RewriteRule ^gt/$ gt/gt.php [NC,L]

Alternatively, you can make use of DirectorySlash off and make the trailing slash optional, like so:

Options +FollowSymlinks
DirectorySlash off

RewriteEngine On
RewriteBase /
RewriteRule ^gt/?$ gt/gt.php [NC,L]

Doing either of the above allows you to have multiple rules of a similar nature in a single .htaccess file.

Upvotes: 0

anubhava
anubhava

Reputation: 785128

You can use this rule in /gt/.htaccess:

Options +FollowSymlinks
RewriteEngine On
RewriteBase /gt/

RewriteRule ^/?$ gt.php [L]

Upvotes: 1

Related Questions