Mobile app version of vmapp.org
Login or Join
Sue5673885

: Rewrite rule for pretty URL: redirecting 'index.php?parameter' to 'slug/index.html' My original URL is: http://example.com/video_in.php?video_id=1 and I want my link to be: http://example.com/shaandaar/kinna_sona.html

@Sue5673885

Posted in: #Htaccess #Redirects #Seo #Url

My original URL is:

example.com/video_in.php?video_id=1

and I want my link to be:

example.com/shaandaar/kinna_sona.html

shaandaar and kinna_sona are the slugs in my database.

This is my .htaccess file

AddDefaultCharset utf-8
Options +FollowSymlinks -Indexes
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]

RewriteRule ^artist/(css|js|images)/(.*)?$// [L,QSA]
RewriteRule ^artists/([^/]*).html$ artist_search.php?&letter= [QSA,L]
RewriteRule ^albums/([^/]*).html$ album_search.php?&letter= [QSA,L]
RewriteRule ^artist/([^/]*).html$ artist_in.php?artist_id= [QSA,L]
RewriteRule ^album/([^/]*).html$ album_in.php?&album_id= [QSA,L]
RewriteRule ^([^/]*)/([^/]*).html$ song_in.php?album_id=&song_id= [QSA,L]
RewriteRule ^([^/]*)/([^/]*).html$ video_in.php?album_id=&video_id= [QSA,L]


all the links are working axceppt video_in.php

What should I write in my *.htaccess* file?

10.02% popularity Vote Up Vote Down


Login to follow query

More posts by @Sue5673885

2 Comments

Sorted by latest first Latest Oldest Best

 

@Jamie184

RewriteRule ^([^/]*)/([^/]*).html$ song_in.php?album_id=&song_id= [QSA,L]
RewriteRule ^([^/]*)/([^/]*).html$ video_in.php?album_id=&video_id= [QSA,L]


You have a conflict with the last two directives. You are using the same pattern for both, so all these requests are going to song_in.php. The second (video_in.php) directive is never going to be processed.

Assuming /shaandaar/kinna_sona.html (for example) could refer to either a song or a video then you are going to have to differentiate between songs and videos in the URL.

10% popularity Vote Up Vote Down


 

@Angela700

and I want my link to be: example.com/shaandaar/kinna_sona.html
shaandaar and kinna_sona are the slugs in my database. What should I write in my .htaccess file?


This is the best way:

RewriteEngine On
RewriteRule ^([^/]+)/([^/]+).html$ /video_in.php?firstslug=&secondslug= [L]


In the RewriteRule, the ^ starts the matching. This: ([^/]+) is broken down as follows:

[^/] means match anything except the /. Its a good way to not confuse the rewrite engine.

The plus means match one or more. So [^/]+ means match any number of characters as long as there is no /.

Then I put the whole thing in brackets so I can use it later. The first set of brackets is #1 , and the second set is #2 .

then I added a backslash in front of the dot because its a way of specifying a dot as a literal character to match.

Then of course the html to match that as well.

To end the matching, I use the $.

When the match is found, /video_in.php?firstslug=(first captured value)&secondslug=(second captured value) will load in the background while the friendly URL stays in the web browser.

In your case and if you use your URL, the values are then shaandaar and kinna_sona respectively. then you can use your script to read the values.

If you want to go paranoid style, you can use this script to make sure your values are processed correctly:

<?php
print_r($_GET);
exit();
?>


When the script runs, you should see an array with your name and value pairs, one per line.

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme