-1

I'm struggling pretty hard with .htaccess. Even though I looked up quite a few solutions here, I couldn't get to setup the following:

I have a file called index.php. Inside the index.php I have a link like

<a href="post/12345678901">Post</a>

Clicking the link, should lead to a file in the same directory called post.php. Inside the post.php I'm grabbing the id through $GET['id'].

But I still like to display post/12345678901 as the URL.

I already tried editing the .htaccess but clicking the links leads to a 404.

Options +FollowSymlinks
RewriteEngine on

RewriteRule ^post/([^/]+) /post.php?id=$1
  • Does this answer your question? [Pretty URLs with .htaccess](https://stackoverflow.com/questions/25080835/pretty-urls-with-htaccess) – admcfajn Mar 10 '21 at 18:23
  • Disable `MultiViews`. That is necessary, when you have such an “overlap” between the fake path (`/post`) and an existing file (`/post.php`). – CBroe Mar 11 '21 at 08:20
  • Please share more details - what do you mean by "I still like to display post/12345678901 as the URL"? – Nico Haase Mar 11 '21 at 10:54
  • @NicoHaase I wanted the URL to look like "post/12345678901". Please check out Martin's solution below. –  Mar 14 '21 at 15:45

1 Answers1

1

You have a RewriteRule but you probably need a ReWriteCondition to go with it else this rewrite will be applied to every call to a page on the apache.

Options +FollowSymlinks
RewriteEngine on

# Not a file
RewriteCond %{REQUEST_FILENAME} !-f
# not a directory
RewriteCond %{REQUEST_FILENAME} !-d 

# Edited to show root location of files. 
# also added NoCaseSensitivity flag.
RewriteRule ^post/([0-9]+)/? /post.php?id=$1 [NC]

Also remove the first / in the second part of the RewriteRule because that will be domain.com/post.php which might not be your actual file location.

Your HTML appears to be showing relative filepathing which is not a good idea and might come back to bite you in the future, As a recommendation every URL on the same domain on your HTML should start with a / so;

<a href="/post/12345678901">Post</a>

Which can then be manipulated by the .htaccess to go where you need, even in another folder (not the base).

Martin
  • 22,212
  • 11
  • 70
  • 132
  • I added a "/" to my href and copy/pasted your code for the .htaccess, but unfortunately again 404. My .htaccess is in the same root folder as index.php and post.php. Is this right? –  Mar 10 '21 at 18:32
  • @arif Does it work to access `/post.php?id=` directly? – Martin Mar 10 '21 at 18:34
  • @arif does that help? – Martin Mar 10 '21 at 18:43
  • Unfortunately, not. I'll contact my hosting provider to see if they restricted access to the .htaccess file. Seems pretty strange that changing the .htaccess file doesn't show any effect. I'll get back as soon as I get a reply! –  Mar 10 '21 at 18:45
  • @arif I have noted in the past that often you will need to force refresh the browser cache when reloading htaccess outputs. So try CTRL+F5 refresh on your page and see if that makes a difference. The htaccess I posted should work for you unless there's something else going on that I'm unaware of. Cheers – Martin Mar 10 '21 at 18:47
  • 1
    @arif you can test if your `.htaccess` file is being read by simply forcing an error -- write some random rubbish in the file and upload it and then try and refresh your website. If the page loads ok then your htaccess is not being read. – Martin Mar 10 '21 at 18:49
  • Seems like there was a problem with my hostin provider! Now it works, but I had to remove the # comments after !-f and !-d. Do you know why? And should I also use the "/" when linking files inside the same folder? Like "/image.png" or "image.png"? –  Mar 11 '21 at 09:14
  • 1
    @arif sorry, yes that's my mistake, comments needed to be on separate lines. I have updated my code. I think for best practise always link to the root (so yes, use `/`) but glad you got it sorted! Cheers – Martin Mar 11 '21 at 10:52