0

I want to replace ?id= and .php with slash / in my post url.

I tried many answers from other questions but did not work for me like this one -

https://webmasters.stackexchange.com/questions/56411/remove-php-and-id-from-url-and-replace-with-slash/79438

Example Url - 
https://example.com/testing/events/news/post.php?id=13/Checking-to-see-if-this-works

I want this example url to be like this

https://example.com/testing/events/news/post/13/Checking-to-see-if-this-works

I used following htaccess code and it removes .php extension from url but can't figure out how to replace ?id= with /

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [QSA,L]

output -

https://example.com/testing/events/news/post?id=13/Checking-to-see-if-this-works

I still can't replace ?id= with /

Thanks for your help :)

3
  • Writing that you "tried many answers from other questions" & giving one is clearly not a case of following How to Ask about research. Why do you think it is? (Rhetorical.) Commented Jun 13, 2020 at 2:43
  • philipxy ! I can list all of the links to answers but it would be very long list. :) Commented Jun 13, 2020 at 2:45
  • To "list all of the links to answers" is clearly not a case of following How to Ask about research. You clearly haven't paid attention to my rhetorical question or the help center. Then you wonder why your questions are poorly received? (Rhetorical.) Commented Jun 13, 2020 at 2:46

1 Answer 1

1

You may use these rules in your site root .htaccess:

Options -MultiViews
RewriteEngine On

# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/+(testing/events/news/post)\.php\?id=([^\s&]+) [NC]
RewriteRule ^ /%1/%2? [R=301,L,NE]

# internal forward from pretty URL to actual one
RewriteRule ^(post)/(.+)/?$ $1.php?id=$2 [L,QSA,NC]

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

6 Comments

I tried this code. url changes but It gives 404 error. Perhaps something needs to changed on post.php. since i get id like this - if (isset($_GET['id'])){ $id = $_GET['id']; }
Where is your htaccess file located? Is it in testing/ directory?
htaccess is located here - /home/username/public_html/testing/events/news/.htaccess
Posted full .htaccess. Make sure you don't have any other rule when testing.
It seems to be working. I am just testing more to make sure it did not break any other page urls or create duplicate urls :)
|

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.