2

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

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)/(.*)?$/$1/$2 [L,QSA]
 RewriteRule ^artists/([^/]*)\.html$ artist_search.php?&letter=$1 [QSA,L]
 RewriteRule ^albums/([^/]*)\.html$ album_search.php?&letter=$1 [QSA,L]
 RewriteRule ^artist/([^/]*)\.html$ artist_in.php?artist_id=$1 [QSA,L]
 RewriteRule ^album/([^/]*)\.html$ album_in.php?&album_id=$1 [QSA,L]
 RewriteRule ^([^/]*)/([^/]*)\.html$ song_in.php?album_id=$1&song_id=$2   [QSA,L]
 RewriteRule ^([^/]*)/([^/]*)\.html$ video_in.php?album_id=$1&video_id=$2 [QSA,L]

all the links are working axceppt video_in.php

    What should I write in my *.htaccess* file?
  • What have you tried? How do you think this might work? If the video-id is required for the real (ugly) URL, but isn't present in the "pretty" URL, where is it supposed to come from? – MrWhite Feb 4 '16 at 8:29
  • you can see here what i have tried till now – Aman patel Feb 5 '16 at 5:25
2

and I want my link to be: http://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=$1&secondslug=$2 [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.

  • HEY....I am not able to redirect properly i have put my code in .htaccess file please see that and help me out. all links are working but this is not the last one.video_in.php is not working – Aman patel Feb 4 '16 at 11:12
  • You might want to try starting over on a blank .htaccess file because you might have some other apache directives in that file (or even in the main config file) that are processing your test URLs instead of the one I showed. – Mike Feb 4 '16 at 17:01
  • can we have more than one .htaccess in one site – Aman patel Feb 5 '16 at 5:24
  • yes if parts of your site are in different folders other than document root folder. But its not a bad idea to make a folder, put .htaccess in it and play around with it. – Mike Feb 5 '16 at 5:46
  • my .htaccess file is in root folder what shall i do?? – Aman patel Feb 5 '16 at 6:19
0

RewriteRule ^([^/]*)/([^/]*)\.html$ song_in.php?album_id=$1&song_id=$2 [QSA,L] RewriteRule ^([^/]*)/([^/]*)\.html$ video_in.php?album_id=$1&video_id=$2 [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.

  • thanks dude....can u please tell me if i want example.com/contactus.php to be example.com/love-to-hear-from-you.html. what shall i do for these problem – Aman patel Feb 5 '16 at 12:44
  • Sorry, I'm not sure that I follow. If you are suggesting that the URL for your contact page should be love-to-hear-from-you instead of contactus - I would stick with contactus. (?!) – MrWhite Feb 5 '16 at 12:51
  • please dude tell me is it possible or not – Aman patel Feb 5 '16 at 12:54
  • Sorry, I don't know what you mean. Are you asking whether the URL /love-to-hear-from-you.html could load the file (ie. internally rewrite to) /contactus.php? – MrWhite Feb 5 '16 at 13:03
  • thank you brother actually i succeeded in that. thank you so much.....yes I want these just display in address bar.any ways thank you – Aman patel Feb 5 '16 at 13:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.