Indy
Indy

Reputation: 4957

.htaccess redirect but keep url

I have the following code and I am trying to do something like that. Let's say I have an address - mydomain.com/server2/ when I type anything after slash for example mydomain.com/server2/whatever I want to load server2.php but address has to be the same

<IfModule mod_rewrite.c>
    Options +FollowSymlinks
  # Options +SymLinksIfOwnerMatch
    RewriteEngine On
    RewriteBase /

    # remove index.php
    RewriteRule ^index\.php/?$ / [L,R=301,NC]

    # Hide File Extensions
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}\.php -f
    RewriteRule ^([^/]+)/$ $1.php

    # Add 301 redirects to new extensionless file
    RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*[^.]+\.php(\?[^\ ]*)?\ HTTP/
    RewriteRule ^(([^/]+/)*[^.]+)\.php$ http://www.mydomain.com/$1 [R=301,L]

    # Add the trailing slash
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
    RewriteRule (.*)$ http://www.mydomain.com/$1/ [R=301,L]


</IfModule>

also I don't know if above code is 100% correct. Thanks for any help.

Upvotes: 0

Views: 829

Answers (1)

anubhava
anubhava

Reputation: 784918

There are problems in your code and in certain URLs it will give you infinite looping. Replace your code with this:

<IfModule mod_rewrite.c>
    Options +FollowSymlinks
    RewriteEngine On
    RewriteBase /

    # remove index.php
    RewriteCond %{THE_REQUEST} /index\.php [NC]
    RewriteRule ^(.*?)index\.php$ /$1 [L,R=301,NC,NE]

    # To externally redirect /dir/foo.php to /dir/foo
    RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(.+?)\.php[\s?] [NC]
    RewriteRule ^ /%1/ [R=301,L,NE]

    # To internally forward /dir/foo to /dir/foo.php
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{DOCUMENT_ROOT}/$1.php -f
    RewriteRule ^(.+?)/?$ /$1.php [L]

    # Add the trailing slash
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule [^/]$ %{REQUEST_URI}/ [L,R=301]

</IfModule>

Upvotes: 1

Related Questions