1

I have three pieces of code.

Here's the first one:

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

The second one:

RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule [^/]$ %{REQUEST_URI}/ [L,R,NE]

The third one:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-_]+)/$ profile/index.php?username=$1 [QSA,L]
  1. The first removes the .php extension from the URL bar
  2. The third shows the contents of a directory. For example, example.com/billgates/ shows the page content of: example.com/profile/index.php?username=billgates
  3. The second adds a trailing slash and redirects the non-trailing slash of the #2. For example, example.com/billgates redirects to example.com/billgates/.

All codes work but just not very nicely together. Here is the problem:

If I go to example.com/login it removes the .php extension and it works. BUT the problem is, if I manually add a trailing slash at the end of /login/, it redirects to: example.com/login/.php/ but it should redirect me to /login because it is not a directory and just a page. But if /login/ was a directory, then it shouldn't redirect.


Full .htaccess:

RewriteEngine On

ErrorDocument 404 /404.php

...the three pieces of codes above
pixie123
  • 809
  • 3
  • 12
  • 20

1 Answers1

1

Have it this way and test after clearing browser cache:

ErrorDocument 404 /404.php

RewriteEngine On

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule /$ - [L,R=404]

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


RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule [^/]$ %{REQUEST_URI}/ [L,R,NE]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/$ profile/index.php?username=$1 [QSA,L]
anubhava
  • 664,788
  • 59
  • 469
  • 547
  • Yes this works, but I don't want it to add trailing slash to all. I just want to add trailing slash to the third one if it is used. So for example, right now it is adding trailing slash to `/login/` but I don't want that. I only want it to add trailing slash to the URL's that use the `/profile/index?php?username=$1` part – pixie123 Dec 13 '19 at 22:52
  • Okay with the updated code, it still does not work. Here are the problems. `/login` goes to `/login/` but I do not want a trailing slash for regular file pages, only for `/profile/index?php?username=$1` pages. Also, `/billgates` and `/billgates/` both work. I was thinking, referring to my original post, is it possible to group #2 and #3 pieces of code together? So if #3 is true, then we use #2? Is that possible? – pixie123 Dec 13 '19 at 23:43
  • Let"s not complicate too much and try to first solve /login issue. /login is not a directory or a file right? Have you cleared browser cache? – anubhava Dec 14 '19 at 04:32
  • Yes, I already tried to clear the browser cache. Yes /login is a file, not directory – pixie123 Dec 14 '19 at 12:07
  • Great, everything works except one thing - `/login` works but if I type `/login/`, it works too. But since login is a file, not directory, it should show 404 page for `/login/` – pixie123 Dec 14 '19 at 12:37
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/204254/discussion-between-pixie123-and-anubhava). – pixie123 Dec 14 '19 at 12:46
  • Thanks for the help, everything is working but there is a problem. I have a file named `test` and a directory named `test`. but when I visit `/test`, it will redirect me to `/test/` (because it is a directory) but if I delete the directory `test` on my server and I try to visit `/test` file, it redirects me to `/test/`and shows 404 error because of caching but it show show the file `test`. How can I disable caching? – pixie123 Dec 15 '19 at 21:18
  • Caching is done in your browser. You can just clear it completely or test from a new browser. For disabling browser cache for all browsers see this: https://stackoverflow.com/questions/11532636/how-to-prevent-http-file-caching-in-apache-httpd-mamp – anubhava Dec 16 '19 at 05:12