Bilal Ozturk
Bilal Ozturk

Reputation: 29

htaccess rewrite rule seo friendly not working

I want to change this URL:

example.com/services-details.php?service_id=1

To:

example.com/services-details/1

Here is the code for .htaccess file

Options +FollowSymLinks -MultiViews
RewriteEngine On

#SEO FRIENDLY URL

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9_-]+)$ services-details.php?service_id=$1

Update (fixed)

Step 1: Add this code in your .htaccess file

Options +FollowSymLinks -MultiViews
RewriteEngine On

# SEO FRIENDLY URL

# Redirect "/services-details.php?service_id=<num>" to "/services-details/<num>"
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} ^service_id=(\d+)$
RewriteRule ^(services-details)\.php$ /$1/%1 [QSD,R=301,L]

# Rewrite "/services-details/<num>" back to "services-details.php?service_id=<num>"
RewriteRule ^(services-details)/(\d+)$ $1.php?service_id=$2 [L]

Step 2: Add this code (on the top) inside the ` HTML element

<base href="/">

After the followed steps it should work.

Info

Don't forget to change the link name services-details!

Upvotes: 1

Views: 307

Answers (1)

MrWhite
MrWhite

Reputation: 45829

RewriteRule ^([a-zA-Z0-9_-]+)$ services-details.php?service_id=$1

The RewriteRule pattern (ie. ^([a-zA-Z0-9_-]+)$) is only matching a single path-segment, whereas the requested URL has two, ie. /services-details/1 - you need to match service-details.

And I'm assuming the service_id is always numeric.

For example:

Options +FollowSymLinks -MultiViews
RewriteEngine On

# SEO FRIENDLY URL

RewriteRule ^(services-details)/(\d+)$ $1.php?service_id=$2 [L]

Since you are rewriting to the same, you might as well capture service-details to avoid repetition.

There would seem to be no need for the filesystem checks, since I don't imagine /services-details/<number> to match a file or directory?


Note that if you are changing an existing URL structure where the old URLs have been indexed by search engines or linked to by third parties then you should also implement a 301 redirect from the old URL (ie. /services-details.php?service_id=<number>) to the new URL (ie. /services-details/<number>) in order to preserve SEO.

For example, the following "redirect" would need to go before the existing "rewrite" (above):

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} ^service_id=(\d+)$
RewriteRule ^(services-details)\.php$ /$1/%1 [QSD,R=301,L]

The QSD flag is required to remove the original query string from from the redirect response.

Summary

Your .htaccess file should look like this with the rules combined:

Options +FollowSymLinks -MultiViews
RewriteEngine On

# SEO FRIENDLY URL

# Redirect "/services-details.php?service_id=<num>" to "/services-details/<num>"
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} ^service_id=(\d+)$
RewriteRule ^(services-details)\.php$ /$1/%1 [QSD,R=301,L]

# Rewrite "/services-details/<num>" back to "services-details.php?service_id=<num>"
RewriteRule ^(services-details)/(\d+)$ $1.php?service_id=$2 [L]

Upvotes: 2

Related Questions