1

For example I want to change:

www.example.com/forum/thread?id=1&topic=hello

to

www.example.com/forum/thread/1/hello

I've looked around and modified my .htacess file to look like this to modify these URLs

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^thread/([0-9]+)/([0-9a-zA-Z_-]+) thread.php?id=$1&topic=$2 [NC,L]

I keep getting a 404 error saying that the file doesn't exist. I'm wondering if it's because I'm removing the .php from the file first using this:

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

But then when I remove that rule and go to thread.php?id=1&topic=hello it just breaks everything and gives me masses of errors

8
  • In which folder have you stored that file? Do you use RewriteBase? Commented Dec 13, 2018 at 15:22
  • by using this rule RewriteRule ^([^\.]+)$ $1.php [NC,L] you are basically turning www.example.com/forum/thread/1/hello into www.example.com/forum/thread/1/hello.php, you need a better regexp to remove the ".php" from URL and have your arguments passed like you want Commented Dec 13, 2018 at 15:23
  • I don't understand. URL rewriting goes usually the other way around. Commented Dec 13, 2018 at 15:24
  • I found that from this website and so far whenever I've added .php to the end of a url it removes it @Kaddath Commented Dec 13, 2018 at 15:24
  • the file is in a folder called forum and that is inside the root and I don't use RewriteBase @NicoHaase Commented Dec 13, 2018 at 15:40

2 Answers 2

1

In your question, you state that you want to change:

www.example.com/forum/thread?id=1&topic=hello to www.example.com/forum/thread/1/hello

However, that is backwards.

The address that you rewrite to needs to be the address that the server can interpret. So, www.example.com/forum/thread/1/hello should be the "friendly" address that the user enters and the rewrite should add the .php extension (although with a rewrite, this will not be visible to the user in the address bar)

Try this:

RewriteEngine on RewriteRule ^/?thread/([0-9]+)/([0-9a-zA-Z_-]+) /thread.php?id=$1&topic=$2

The reason that you are probably getting errors going to thread.php is that you have a rule to remove the .php (which now the server will not be able to render the page). With the above rewrite rule, you will get to it by going to : www.something.com/thread/1/hello

Sign up to request clarification or add additional context in comments.

8 Comments

The previous RewriteRule works but now it thinks that thread is a directory and not a .php file, it thinks that the hello is a .php file
In the example above, you have $1.php. That will add a .php to whatever the first variable in your rewrite rule is. You want to make sure in the rewrite rule that you specify the php page you should go to (ex. thread.php) then assign the variables to the php variables. (ex. id=$1&topic=$2).
Yeah my code does say that, it's pretty much the exact same but you've added a slash and question mark
Did you remove the other rule that adds the .php?
Yes I did, I'm using this RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME}\.php -f RewriteRule ^/?thread/([0-9]+)/([0-9a-zA-Z_-]+) /thread.php?id=$1&topic=$2 only now
|
0

Your rule will never get applied because you are using RewriteCond %{REQUEST_FILENAME}\.php -f this condition checks your filesystem to see if /forum/thread/1/hello.php exists and if it doesn't then your rule is ignored. Simply comment out or remove that condition from your rule .

Try :

RewriteCond %{REQUEST_FILENAME} !-d
#RewriteCond %{REQUEST_FILENAME}\.php -f
 RewriteRule ^thread/([0-9]+)/([0-9a-zA-Z_-]+) thread.php?id=$1&topic=$2 [NC,L]

1 Comment

Didn't work either, every single suggestion isn't working

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.