-1

I have the folloowing code for .htaccess

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

## hide .php extension
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^profile/

## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_FILENAME}.php [L]

##hide id parameter
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([A-Za-z0-9_])/([^/]*)$ /$1.php?id=$2 [L]

2 of my functions work properly, the hide id function however does not work, it should do the following: post.php?id=7 turned to post/7.

I am not very experienced with .htaccess. In fact I am unfamiliar with it, so I wanted to know how would I solve the issue of the above. Also this must not affect my POST and GET requests that I use as I have a ton of AJAX requests throughout every single page of the website.

1 Answers1

0

You're close:

##hide id parameter
RewriteRule ^post/(\d+)$ post.php?id=$1 [L]

The pattern ^post/(\d+)$ matches URLs starting with "post/" followed by one or more digits. The (\d+) captures the digits and assigns them to the $1 backreference. The target post.php?id=$1 rewrites the URL to the original form but with the captured digits as the "id" parameter. The [L] flag indicates that this is the last rule to be processed.

James
  • 834
  • 7
  • 27