Code Road
Code Road

Reputation: 91

not able to convert dynamic url to static url via .htaccess

I am rewriting my problem . Below I am giving all the details of what i am trying to do, and what is the result I am getting. I hope to be very clear this time. I have a samples page here www.broadcast2world.com/samples.php. All the links and data which we see on the page is coming from a database. Now suppose we click on a link "More Info" for a particular video say social control. A specific href for that video is triggered as <a href="video.php?pid='.$id.'"> . It passes its id over url in the format http://www.broadcast2world.com/video.php?pid=66.

I catch that id into a variable using

if(!isset($_GET['pid'])){
    $pageid='66';
}
else{
  $pageid=$_GET['pid'];  
} 

Using the $pageid variable, I run a query which ultimately prepare the page for that particular video.

What my SEO guy need is a url in the format www.broadcast2world.com/video/name-of-the-video.

Now the problem is that if I make a mod_rewrite amendments which is wonderfully explained by Ben below, I am not able to figure out, how to link that name-of-the-video to its id. I am sure there must be something I have to modify while creating specific url for videos as explained above. But I really don't know what? Thanks again for your wonderful support

Upvotes: 0

Views: 434

Answers (2)

Ben Swinburne
Ben Swinburne

Reputation: 26477

There are a number of things wrong with the .htaccess code generated by that website.

  • The period character (.) should be escaped on the left hand side of RewriteRule directives - i.e. \.php.
  • The query string isn't taken into account (This is the primary cause of your problem which I explain below).
  • There's no indication of the type of redirect used. Temporary? Permanent?
  • There's no RewriteBase directive. Not essential as / is taken as the default, but for readability's sake, I tend to use it.

Rewrite directives notably take into account only the REQUEST_URI component. The below is from the mod_rewrite manual page.

REQUEST_URI The path component of the requested URI, such as "/index.html". This notably excludes the query string which is available as as its own variable named QUERY_STRING.

The following code is explained in the comments, and debugging info from here can be found below.

# Enable rewrite engine http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewriteengine
RewriteEngine On

# Sets the base URL for per-directory rewrite http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewritebase
RewriteBase /

# Match the regular expression ^pname=...$ with the QUERY_STRING variable as the subject
# Query string starts with pname (`^`). 
# Matches exactly pname=overviewvideos.
# Must end with overviewvideos (`$`)
RewriteCond %{QUERY_STRING} ^pname=overvidevideos$

# If the above condition is met, redirect to the 2nd parameter.
# Note the escaped period (\.)
# Note the status code 302 (Found, temporary) Can be 301 for permanent etc.
# Note the L flag - this prevents further rules being processed if the conditions are met. (L for Last)
# Note that at this stage we are back to matching the REQUEST_URI, so just allvideos.php in our regular expression. Must start and end with the string allvideos.php
RewriteRule ^allvideos\.php$ allvideos.php?pname=Overview%20Videos [R=302,L]

Input URL: http://www.yoursite.com/allvideos.php?pname=overvidevideos

RewriteCond %{QUERY_STRING} ^pname=overvidevideos$  This condition was met
RewriteRule ^allvideos\.php$ allvideos.php?pname=Overview%20Videos [R=302,L]    
    This rule was met, the new url is http://www.yoursite.com/allvideos.php?pname=Overview%20Videos
    Test are stopped, because of the R in your RewriteRule options. A redirect will be made with status code 302

Output URL: http://www.yoursite.com/allvideos.php?pname=Overview%20Videos

To repeat the rules, you can duplicate the lines beginning RewriteCond and RewriteRule.

Upvotes: 2

Taha Paksu
Taha Paksu

Reputation: 15616

Replace the following line

RewriteRule ^allvideos.php/pname=overviewvideos$ allvideos.php?pname=Overview%20Videos 

With the below code

RewriteRule ^allvideos.php?pname=overviewvideos$ allvideos.php?pname=Overview%20Videos 

Note the replacement of the / with a ?

Similarly make the same alteration to the 2nd rewrite rule

Upvotes: 1

Related Questions