Magical Tomato
Magical Tomato

Reputation: 101

rid of id number in link address using .htaccess

I have this link that works and leads to the correct article:

<a href="https://www.example.com/news/21/Hello-World">Article</a>
however I do not want the link to display the id number I want the link to look like this:

<a href="https://www.example.com/news/Hello-World">Article</a>
This is what I have in my .htaccess file, any help would be appreciated, thanks:

Options -MultiViews
ReWriteEngine On
RewriteBase /
RewriteRule ^news/(\d+)/([\w-]+)/?$ news.php?id=$1&linkAddress=$2 [NC,L,NE]

Is it poosible to remove the 21 id from the link but still use the id to get the variables from database. thanks.

Upvotes: 2

Views: 124

Answers (1)

RavinderSingh13
RavinderSingh13

Reputation: 133428

Could you please try following, written based on samples. Please make sure you clear your browser cache after placing these urls in your .htaccess file. Also make sure this new rule is above your already existing news.php rule(shown in your samples).

RewriteEngine ON
RewriteRule ^(news)/(Hello-World)/?$ $1/21/$2 [NC,L]

##Placing OP's rule to serve urls starting with news with news.php in backend.
RewriteRule ^news/(\d+)/([\w-]+)/?$ news.php?id=$1&linkAddress=$2 [NC,L,NE]

Upvotes: 3

Related Questions