Search code examples
phpvanity-url

Vanity Url ID issue


I am trying to do vanity url.

I want this http://mydomain.com/series/profile.php?profile_id=751130015 to become something like this

http://mydomain.com/series/751130015

I've tried creating a new file '.htaccess' on the directory and add this code:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /profile.php?profile_id=$1

Yet, I am still getting an error when I try to acccess

http://mydomain.com/series/751130015

Am I missing anything?

The error I am getting is:

Object not found!

The requested URL was not found on this server. If you entered the URL manually please check your spelling and try again.

If you think this is a server error, please contact the webmaster.

Error 404


Solution

  • RewriteEngine On
    RewriteRule ^series/([0-9]+)$ /series/profile.php?profile_id=$1 [L]
    

    explanation:

    anything that starts (^) with "series" then a dash (/) followed by numbers ( ([0-9]+) ) - also remember these numbers as the first variable "$1" and eventually ending ($) will be redirected to /series/profile.php?profile_id=$1

    so http://mydomain.com/series/751130015

    will be redirected to http://mydomain.com/series/profile.php?profile_id=751130015

    UPDATE: the other way around:

    RewriteEngine On
    RewriteCond %{REQUEST_URI}  ^/series/profile\.php$
    RewriteCond %{QUERY_STRING} ^profile_id=([0-9]*)$
    RewriteRule ^(.*)$ http://mydomain.com/series/%1? [R=301,L]
    

    explanation (first condition) if /series/profile.php is called with (second condition) parameters starting with "profile_id=" and is followed by numbers then do the rewrite like this: go to http://mydomain.com/series/ and add whatever the numbers were at the end and stop there (?) -we intentionally stop there to avoid query_string appended to rewrite result. inform the visitor that this is a PERMANENT REDIRECT with status 301. Then terminate the htaccess execution (L)

    so http://mydomain.com/series/profile.php?profile_id=751130015 will redirect (R=301 - PERMANENT REDIRECT) to http://mydomain.com/series/751130015

    (you can use R=302 for temporary redirect)