How to rewrite the URL of file which is in the subfolder using htaccess

Viewed 46

I'm trying to rewrite this URL : http://localhost/mysite/admin/edit-post.php?id=12

Expected Output URL : http://localhost/mysite/admin/edit/12

mysite is my rootfolder

admin is my subfolder which inside my rootfolder

edit-post.php file is inside my subfolder

My htaccess code:

# 404 page
ErrorDocument 404 http://localhost/mysite/404.php

RewriteEngine On
RewriteBase /mysite/

# Extension Removal
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

# URL Rewrite
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# This Rule is for the file which i have it in my root folder, It's working
RewriteRule ^posts/(.+)$ post.php?id=$1 [L]

# And I'm tring the same but it's not working. 
RewriteRule ^edit/(.+)$ edit-post.php?id=$1 [L]

My php Code

<a href="edit/<?php echo $post_detail['id']; ?>">Edit Post</a>

1 Answers

With your shown attempts, samples; please try following htaccess Rules file.

Please make sure to clear your browser cache before testing your URLs.

# 404 page
ErrorDocument 404 http://localhost/mysite/404.php

RewriteEngine On
RewriteBase /mysite/

# Extension Removal
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

# URL Rewrite
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# This Rule is for the file which i have it in my root folder, It's working
RewriteRule ^posts/(.+)$ post.php?id=$1 [L]

###Newly added rule here, where considering that your edit-post.php is present inside admin folder.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^admin/edit/(.+)/?$ admin/edit-post.php?id=$1 [L]
Related