1

After using header('Location:employees');exit; after a POST, I was somehow redirected instead to localhost/project_name/employees/. Even if my <a> points to just employees

<a href="employees" role="button" class="col-1 my-1 mr-1 btn btn-outline-primary btn-sm">Employee</a>

I'm always redirected to employees/. This does not happen to the other links, and if I use employee, I'm being redirected correctly, no trailing slash being added. I suspect it is because of my .htaccess, but it does not affect other link.

Here's my .htaccess:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

Even if I delete the contents, it still adds a trailing slash to employees. I already tried clearing the browser's data and opening it in another browser (Microsoft Edge), still the same result.

I tried updating htaccess with the answer here, but still no success

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

I also tried clearing all the contents of employees.php just to see if it because of my codes, but the problem still persist.

How can I prevent this from happening to the other links? I can't seem to reverse it, and the solution that I managed to find is to just change the name. Although it is not a problem, I want to know the cause of this.

Carl Binalla
  • 5,393
  • 5
  • 27
  • 46

2 Answers2

1

Converting my comments to answer.

None of the rules shown in question would add a trailing slash. It could be due to some code or Apache server directives.

To verify this, test in Chrome dev tool with caching disabled and check in Networking tab what are 3xx/4xx URLs it shows there.

Another thing to note is if your URI points to a real directory then Apache's mod_dir module adds a trailing directory and sends a 301 to clients for security reasons.

anubhava
  • 761,203
  • 64
  • 569
  • 643
0

can you try this .htaccess: (backup your .htaccess first, then overwrite it with these lines)

RewriteEngine On    
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /project_name/$1 [L,R]

I just tested it on my server, it works!

Ray A
  • 1,283
  • 2
  • 10
  • 22
  • The error went from **Object not found** to **Access Forbidden** – Carl Binalla Aug 13 '18 at 07:47
  • check the updated code, overwrite your .htaccess after you take a backup of it. – Ray A Aug 13 '18 at 08:01
  • Well, I can't now open the links because they are now looking for the `.php` extension. And regarding the `employees`, it is still being redirected to `employees/`. One thing I noticed is that somehow, a folder named `employees` had been created – Carl Binalla Aug 13 '18 at 08:06
  • Wait, I'm actually being redirected to `localhost/employees` instead of `localhost/project_name/employees`. I tried using `/$1.php` instead of `/$1`, and it became `localhost/employees.php` while the other links does not have the `.php` extension when being redirected – Carl Binalla Aug 13 '18 at 08:09