1

I want to rewrite these .htaccess rules into a templates to use it for my other pages as well:

RewriteRule ^admin/add-news/?$ admin/add_news.php [L] RewriteRule ^admin/edit-news/([0-9a-z-@._]+)/([0-9]+)/?$ admin/edit_news.php?name=$1&id=$2 [L] 

For example:

RewriteRule ^admin/([a-z-])/?$ admin/$1.php [L] RewriteRule ^admin/([a-z-])/([0-9a-z-@._]+)/([0-9]+)/?$ admin/$1.php?name=$2&id=$3 [L] 

I tested the latest rules on WAMP and it does not work. It returns: 404 Not Found error

The requested URL was not found on this server.

The full .htaccess:

RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME}\.php -f RewriteRule ^(.*)$ $1.php RewriteRule ^admin/add-news/?$ admin/add_news.php [L] RewriteRule ^admin/edit-news/([0-9a-z-@._]+)/([0-9]+)/?$ admin/edit_news.php?name=$1&id=$2 [L] 

Files locations are:

1. C:\wamp64\www\test.com 2. C:\wamp64\www\test.com\admin\add_news.php C:\wamp64\www\test.com\admin\edit_news.php 3. I want redirect from URLs with underscores to hyphens, for example: from admin/add_news.php to admin/add-news Also from admin/edit_news.php?name=%1&id=%2 to admin/edit-news/name/id Example: admin/edit-news/my-news-title/2 

Any ideas how to fix it? Thank you.

4
  • Thanks for showing your efforts, could you please do let us know 3 things here. 1st- Where is your htaccess file present(FULL path details), 2nd- where are your .php files present(complete path please)? 3rd- What sample urls you are hitting? and FROM which sample url TO which sample url you want to rewrite/redirect? Thank you. Commented Nov 19, 2021 at 22:18
  • 1
    @RavinderSingh13 Ok. 1. C:\wamp64\www\test.com 2. C:\wamp64\www\test.com\admin\add_news.php C:\wamp64\www\test.com\admin\edit_news.php 3. I want redirect from URLs with underscores to hyphens, for example: from admin/add_news.php to admin/add-news Also from admin/edit_news.php?name=%1&id=%2 to admin/edit-news/name/id Thank you. Commented Nov 19, 2021 at 22:26
  • Thanks for letting info, so you are hitting link admin/edit-news/name/id in browser? If yes then please do add this all information in your question to make it more clear, thank you. Commented Nov 19, 2021 at 22:27
  • @RavinderSingh13 Yes, browser url must be for example: admin/edit-news/my-news-title/2. Commented Nov 19, 2021 at 22:29

1 Answer 1

2

With your shown attempts, please try following htaccess rules file.

Make sure that your .htaccess is present along with your admin folder AND make sure to clear your browser cache before testing your URLs.

Samples specific rules:

RewriteEngine ON RewriteBase /test.com/ ##First rule for hitting url admin/add-news in browser. RewriteRule ^test\.com/admin/([\w-]+)/?$ admin/$1.php [QSA,NC,L] ##Second rule for hitting url admin/edit-news/name/id in browser. RewriteRule ^test\.com/admin/([\w-]+)/([^/]*)/(\d)+/?$ admin/$1.php?name=$2&id=$3 [QSA,NC,L] 

Generic rules:

RewriteEngine ON RewriteBase /test.com/ ##First rule for hitting url admin/add-news in browser. RewriteRule ^test\.com/admin/([^-]*)-([^/]*)/?$ admin/$1_$2.php [QSA,NC,L] ##Second rule for hitting url admin/edit-news/name/id in browser. RewriteRule ^test\.com/admin/([^-]*)-([^/]*)/([^/]*)/(\d)+/?$ admin/$1_$2.php?name=$3&id=$4 [QSA,NC,L] 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.