0

my current url : http://example.com/blog-single.php?id=15

this is a dynamic blog page the "id" has to be change accordingly to blog adding

I need to change this url to http://example.com/blog-single/<coresponding-blog-name>

eg: http://example.com/blog-single/my-new-blog

how to Remove .php and id from the current URL?

Amarendra Kumar
  • 858
  • 9
  • 16
smijith
  • 13
  • 7
  • 1
    Check this out. http://stackoverflow.com/questions/7677070/htaccess-rewrite-get-variables http://stackoverflow.com/a/7945816/6700273 – Rohit Dhiman Aug 11 '16 at 05:16

3 Answers3

1

First of you need to create a .htaccess file and add this code in it

RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ blog-single.php?id=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ blog-single.php?id=$1

After that in your main file for example in index.php file you need to add the code

<?php

$key=$_GET['key'];

if($key=='my-new-blog')
{
include('my_new_blog.php'); // Home page
}
else if($key=='login')
{
include('login.php'); // Login page
}
else if($key=='terms')
{
include('terms.php'); // Terms page
}
else
{
include('users.php'); // Users Gateway
}
?>
1

try this according to your need.

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)/([a-zA-Z0-9_-]+)$ $1.php?id=$2 [NC,L]

here you can use your filename in manner http://example.com/blog-single/my-new-blog this will work for every page as per your requirement.

Abhishek Gurjar
  • 7,426
  • 10
  • 37
  • 45
  • url not overriding in webserver – smijith Aug 11 '16 at 12:27
  • okay here 1st make sure the rewritengine is enabled in httpd.conf or please clear what exactly is the need cause when you `http://example.com/blog-single/my-new-blog` in browser it will equate to `http://example.com/blog-single.php?id=value` – Abhishek Gurjar Aug 11 '16 at 12:29
  • above will work for any page on same condition only you need id as your get variable or you can change the parameter yourself. – Abhishek Gurjar Aug 12 '16 at 10:38
  • if you only have filename but not get variable then `RewriteRule ^(.*)$ $1.php [NC,L]` will do the trick but any way above solution is best you can use variable if you want or skip if dont have need. – Abhishek Gurjar Aug 12 '16 at 10:42
  • current my url is 'http://example.com/about.php' & I need to change this to 'http://example.com/about' and also i need to change this url "Blog" 'http://example.com/blog-single.php?id=15' to 'http://example.com/blog-name' Is ther any thing I have to change is pages also without .htaccess file – smijith Aug 12 '16 at 10:44
  • only way i knew to remove extension is to use of .htaccess and i request you to please try my answer i think you are having doubt of working of htaccess http://httpd.apache.org/docs/current/mod/mod_rewrite.html – Abhishek Gurjar Aug 12 '16 at 10:51
0

For example, if you access to this: http://example.com/blog/1234.html and real path is http://example.com/blog.php?id=1234 you can put this on your htaccess file:

RewriteEngine On RewriteRule ^blog/([^/]*)\.html$ /blog.php?id=$1 [L]

This tool will help you: http://www.generateit.net/mod-rewrite/index.php

sloaxleak
  • 106
  • 5