Akshay Shrivastav
Akshay Shrivastav

Reputation: 1227

.htaccess code making a wrong url redirect

This is my .htaccess code

#RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteCond %{HTTP_HOST} !hashstar\.com$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]

#over here we have set the default root directory now request will be directly made from this directory
RewriteCond %{HTTP_HOST} ^hashstar.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.hashstar.com$
RewriteCond %{REQUEST_URI} !app/
RewriteRule (.*) /app/$1 [L]

#redirect all requests except only POST
RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{THE_REQUEST} \s/+(.*?/)?(?:index)?(.*?)\.(?:php?)[\s?/] [NC]
RewriteRule ^ /%1%2 [R=302,L,NE]

RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

#we are setting here the default file to load while the URL is called followed by fallback files
DirectoryIndex index.php timer/index.php

My root directory is set to folder app

In app folder, I have 2 folders

1.) Manage
2.) Timer

To access manage folder I have to type in URL

http://www.example.com/manage/

To access timer folder I have to type in URL

http://www.example.com/timer/

if I use these Url by adding a slash in URL (/) the URL loads perfectly

but if the don't add the (/) in URL then it makes a redirection through the root directory

URL should look like => http://www.example.com/manage

It becomes like => http://www.example.com/app/manage/

What's the reason behind it & how to fix this problem?

Any helps appreciated. Thanks in advance.

Upvotes: 1

Views: 1068

Answers (2)

anubhava
anubhava

Reputation: 785471

Reason of this redirect is that rewritten URI is a real directory without trailing slash. When this happens, Apache's mod_dir module acts on it by redirecting to a URI with a trailing slash.

To prevent use this .htaccess:

#we are setting here the default file to load while the URL is called followed by fallback files
DirectoryIndex index.php

#RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteCond %{HTTP_HOST} !hashstar\.com$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]

#redirect all requests except only POST
RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{THE_REQUEST} \s/+(.*?/)?(?:index)?(.*?)\.(?:php?)[\s?/] [NC]
RewriteRule ^ /%1%2 [R=301,L,NE]

# adds a trailing directory if rewritten URI is a direcory
RewriteCond %{DOCUMENT_ROOT}/app/$1 -d
RewriteRule ^(.*?[^/])$ %{REQUEST_URI}/ [L]

#over here we have set the default root directory now request will be directly made from this directory
RewriteCond %{HTTP_HOST} ^(?:www\.)?hashstar.com$ [NC]
RewriteCond %{REQUEST_URI} !/app/ [NC]
RewriteRule (.*) /app/$1 [L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

Upvotes: 1

SsJVasto
SsJVasto

Reputation: 496

At a glance (I didn't try to reproduce these rules in my Apache2 server), it would seem that this is the culprit:

RewriteRule (.*) /app/$1 [L]

the Regular Expression (.*) will catch Any character greedily. Your Rewrite Rule is simply adding /app/ between the domain and the script.

Find and group <all non-null characters>,
    and transform it to /app/<all non-null characters>

The URL www.example.com/myscript.php would be rewritten as www.example.com/app/myscript.php

EDIT

so what code edit do u suggest? @SsJVasto

You could simply exclude / from the greedy rule:

RewriteRule (.*)/ $1 [L]

This way, you are storing the result of anything ending with a /, but without said / into $1. Since the / isn't inside the group, it will be forgotten.

If you want to handle repetition (like http://www.example.com/myscript.php//), you can add a + sign to handle one or more occurrences:

RewriteRule (.*)/+ $1 [L]

Upvotes: 1

Related Questions