2

I have a .htaccess file:

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

And I have a file called random.php.

All I want to do is just call something.com/random, but Apache adds a trailing slash (using 301 Redirect) to the end of the URL, therefore giving an error stating that something.com/random/.php cannot be found.

EDIT 1: When I use

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

then my external files (.js, .css, etc.) can't load and the server responds with a 500 Internal Server Error response. Apache says that there were too many redirects.

2 Answers 2

2

Try this rule with optional trailing slash:

RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME}.php -f RewriteRule ^(.+?)/?$ $1.php [L] 
Sign up to request clarification or add additional context in comments.

5 Comments

All right, it unfortunately does not work. :( My external stylesheets can't load with 500 Internal Server Error.
Still not working :( There must be something with the apache directory TrailingSlash thing.
Need more info to proceed. Take time and answer all questions. Q1. What URL are you entering in your browser? Q2. Is this the only .htaccess or do you have another? Q3. Is this the only code in this .htaccess? If you have some other code then please post it in question? Q4. Where is above .htaccess located?
Q1: https://www.forcemagic.xyz/random Q2&3: I do not have any more .htaccess files, and there's nothing else written in this. Q4: It's located in /var/www (server root)
Wait... it works... but with only that file. Never mind. Solved it myself, just renamed the file
1

You almost had it right, you just needed to exclude the slash as well:

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

5 Comments

Could you start your .htaccess with Options -MultiViews?
You mean add that to the first line? No, it still doesn't work. I searched and found that there's a Directory TrailingSlash yes|no thing. Shouldn't I set that instead?
See my edited answer, i'm assuming you have more lines in your htaccess? Because you speak of a 301 redirect that you do not give?
See my edited comment, that I think that's the initial problem
You mean DirectoryIndexRedirect? That's only for when you have a folder named /random/ as well. But my code should work if you don't have a folder with that name.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.