0

Hello everyone i have made this application in PHP with different pages linked together with each other. The application is working fine,but the URL generated are a lot messy and complicated. I have three pages in my application namely

1.index.php (the default file)
2.video.php (the category page)
3.video-detail.php(the page for playing the video)

And the name of the root folder is april

Now the links which are generated for the application are.

http://localhost/april/video?cat_id=2&category=Programming-Language

Considering i am in the second page browsing some category after taking some input from the user in the first page. Now i am trying that the URL should be a lot cleaner than this. For eg: the URL should be in this format .

http://localhost/april/Programming-Language.

I know this task is achieved using .htacess, but i can't really understand it's approach. So any help regarding this topic is highly appreciated.

Harjeev Singh
  • 13
  • 1
  • 7

2 Answers2

0

http://localhost/april/video?category=Programming-Language&cat_id=2

This can be transformed into: http://localhost/Programming-Language/2

(notice that I shifted cat_id to the end, since I suspected it to be a page number.)

This is accomplished by placing the following in your .htaccess:

RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)$ /april/video?category=$1&cat_id=$2 [L]

Obviously you can then pick up the variables via $_GET request ($_GET['category'])

sushibrain
  • 2,712
  • 5
  • 33
  • 62
  • tried this approach . did'nt work out .. this is my htaccess file. – Harjeev Singh Apr 21 '17 at 12:43
  • @HarjeevSingh For starters, try it without the rest of the stuff in there, also, make sure you use this properly. – sushibrain Apr 21 '17 at 12:45
  • So i came across this code `RewriteRule ^products/([0-9]+)/?$ show_a_product.php?product_id=$1 ` , but i can't seem to figure what is converting what . I mean the tutorial said that it would look in the URL for a particular pattern and then convert it into the complete messy link. But shouldn't the process be the other way around. – Harjeev Singh Apr 24 '17 at 05:07
  • With the help of .htaccess file i was able to trim down the URL to `http://localhost/april/24_april/24_april/video-library/2/Programming-Language`. Now the issue is every time i run the application it firsts loads the messy URL , and when i edit it accordingly then the page also gets loaded. So is there any way that the application could directly load the clean URL, rather than the messy URL. – Harjeev Singh Apr 24 '17 at 09:47
0

Do the following changes in the .htaccess

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}\.php -f 
RewriteRule ^(.*)$ $1.php

If you want more information check these links

1.http://httpd.apache.org/docs/trunk/mod/mod_dir.html#fallbackresource 2.https://mediatemple.net/community/products/dv/204643270/using-htaccess-rewrite-rules

lalithkumar
  • 3,480
  • 4
  • 24
  • 40