techworld
techworld

Reputation: 331

Pretty url not redirecting, htaccess

I'am quite new with htaccess. I have successfully created .htaccess file with following rules to redirect user with pretty url.

My actual url

http://localhost/domain.com/Job-Details.php?Job_ID=30

What i want to achieve

http://localhost/domain.com/30

My Current htaccess

RewriteEngine On
RewriteBase /domain.com/

# Get the URI-path directly from THE_REQUEST variable
  RewriteCond %{THE_REQUEST} ^(GET|HEAD)\s/(.*)\.php [NC]

# Search friendly URLs for job detail page 
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^([0-9]+)$ Job-Details.php?Job_ID=$1 [NC,L,R=301]

Issues I'am facing

  1. Redirection with pretty url not happening. I'am stil seeing old url
  2. Using mentioned htaccess when i enter pretty url in address bar directly then page displays fine but url gets changed to my current url automatically.

any idea what wrong i'am doing here.

Upvotes: 2

Views: 635

Answers (2)

Mike Rockétt
Mike Rockétt

Reputation: 9007

Please try with the following /domain.com/.htaccess code:

RewriteEngine On
RewriteBase /domain.com/

# Redirect actual URI to SEF URI
RewriteCond %{THE_REQUEST} (?:GET|HEAD)\s\/domain.com\/Job\-Details\.php\?Job_ID\=(\d+) [NC]
RewriteRule ^ %1? [R=302,L,NE]

# SEF URI for Job-Details page
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(\d+)$ Job-Details.php?Job_ID=$1 [NC,QSA,L]

Change R=302 to R=301 if you are happy with the results. Remember to clear your cache before testing this, otherwise your browser may redirect based on historical cache.

Upvotes: 2

Xposedbones
Xposedbones

Reputation: 597

Change this:
RewriteRule ^([0-9]+)$ Job-Details.php?Job_ID=$1 [NC,L,R=301]

RewriteRule ^([0-9]+)$ Job-Details.php?Job_ID=$1 [L,QSA]

With R=301 you were creating a 301 redirection.

Upvotes: 0

Related Questions