2

I am trying to make SEO friendly website using php...

In folder jcheck i have a index.php file which take one parameter "id" as input.

mydomain.com/jcheck/index.php?id=1 works

How can i make it as follow

mydomain.com/jcheck/1

I have tried making .htaccess file and putting

RewriteEngine on

RewriteCond %{REQUEST_URI} 1/
RewriteRule 1/ http://mydomain.com/jcheck/index.php?id=1

How can i make it work?

Giacomo1968
  • 23,903
  • 10
  • 59
  • 92
user1853803
  • 620
  • 2
  • 7
  • 27

5 Answers5

4

You can essentially do this 2 ways:

The .htaccess route with mod_rewrite

Add a file called .htaccess in your root folder, and add something like this:

RewriteEngine on
RewriteRule ^/Some-text-goes-here/([0-9]+)$ /picture.php?id=$1

This will tell Apache to enable mod_rewrite for this folder, and if it gets asked a URL matching the regular expression it rewrites it internally to what you want, without the end user seeing it. Easy, but inflexible, so if you need more power:

The PHP route

Put the following in your .htaccess instead:

FallbackResource index.php

This will tell it to run your index.php for all files it cannot normally find in your site. In there you can then for example:

$path = ltrim($_SERVER['REQUEST_URI'], '/');    // Trim leading slash(es)
$elements = explode('/', $path);                // Split path on slashes
if(count($elements) == 0)                       // No path elements means home
    ShowHomepage();
else switch(array_shift($elements))             // Pop off first item and switch
{
    case 'Some-text-goes-here':
        ShowPicture($elements); // passes rest of parameters to internal function
        break;
    case 'more':
        ...
    default:
        header('HTTP/1.1 404 Not Found');
        Show404Error();
}

This is how big sites and CMS-systems do it, because it allows far more flexibility in parsing URLs, config and database dependent URLs etc. For sporadic usage the hardcoded rewrite rules in .htaccess will do fine though.

*****Copy this content from URL rewriting with PHP**

Community
  • 1
  • 1
Goutam Pal
  • 1,758
  • 1
  • 10
  • 14
  • This is a cool solution the second one, but isn't it slower than the first one? – user18490 Aug 13 '14 at 00:41
  • For me, `count($elements)` contains an empty string for root calls (home). It can be caught with a case for empty string (i.e. `case '':`). Also, `FallbackResource index.php` was failing on root. Instead I got it working with the traditional mod_rewrite from [link](https://www.adayinthelifeof.nl/2012/01/21/apaches-fallbackresource-your-new-htaccess-command/). – Farshid T Feb 22 '15 at 19:38
  • i have many url for same as above. so create one by one define in .htaccess route with mod_rewrite. if you posible one time define. please quik answer – Mehul Velani Apr 26 '19 at 05:08
2

In the htaccess file in your jcheck directory, use these rules:

RewriteEngine On
RewriteBase /jcheck/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9]+)/?$ index.php?id=$1 [L,QSA]
Jon Lin
  • 135,941
  • 26
  • 200
  • 209
1

Try this one:

RewriteRule ^jcheck/([0-9]+)$ jcheck/index.php?id=$1
Bora
  • 9,846
  • 4
  • 39
  • 66
0
RewriteEngine on
RewriteRule ^/jcheck/([0-9]+)$ /jcheck/index.php?id=1

and see also here it will usefull for you

Community
  • 1
  • 1
Rituraj ratan
  • 9,599
  • 7
  • 32
  • 51
0

Put .htaccess file inside jcheck folder and write:

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-D
RewriteRule ^1/?$ index.php?id=1
Jason OOO
  • 3,447
  • 2
  • 21
  • 28