7
  • I am just new to .htaccess.
  • I need some rewrite rules for URLs.
  • I Google'd some and applied but no change in URL.

I want:

demo.example.com/section.php?id=1 

Changed to:

demo.example.com/section/sample-section

i tried

RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f

RewriteRule ^section/(\d+)*$ ./section.php?id=$1

but no difference

Thanks.

I will appreciate your help.

Justin Iurman
  • 18,954
  • 3
  • 35
  • 54
ahmad ali
  • 1,155
  • 1
  • 9
  • 17
  • 2
    possible duplicate of [Reference: mod\_rewrite, URL rewriting and "pretty links" explained](http://stackoverflow.com/questions/20563772/reference-mod-rewrite-url-rewriting-and-pretty-links-explained) – David Jan 27 '15 at 10:36
  • Could you paste your full `.htaccess`? Is the rewrite module installed and enabled? – ojrask Jan 27 '15 at 10:57
  • Links are usually discouraged on SO that's why I am putting this in a comment. A must read for every mod_rewrite beginner. http://code.tutsplus.com/tutorials/an-in-depth-guide-to-mod_rewrite-for-apache--net-6708 –  Jan 27 '15 at 11:19

2 Answers2

12

First, make sure mod_rewrite is enabled and htaccess files allowed in your Apache configuration.

Then, put this code in your htaccess (which has to be in root folder)

Options -MultiViews
RewriteEngine On

# redirect "/section.php?id=xxx" to "/section/xxx"
RewriteCond %{THE_REQUEST} \s/section\.php\?id=([0-9]+)\s [NC]
RewriteRule ^ /section/%1? [R=301,L]

# internally rewrite "/section/xxx" to "/section.php?id=xxx"
RewriteRule ^section/([0-9]+)$ /section.php?id=$1 [L]
Justin Iurman
  • 18,954
  • 3
  • 35
  • 54
  • lurman thank you . it works very fine. one thing i want to ask. Now i have to change my php code accordingly . i mean have to explude and get section title ? – ahmad ali Jan 27 '15 at 11:32
  • Well actually you don't have to change anything since you still get `id` parameter "in the back" (hidden). I'm not sure i understood your question, if that's not the case, please explain – Justin Iurman Jan 27 '15 at 11:36
  • yeas this is what i wanted to know about id . But after apply this htaccess . am getting 404 error. – ahmad ali Jan 27 '15 at 11:39
  • What is exactly the url you tried ? Also, make sure *mod_rewrite* is enabled and *htaccess* allowed in your apache configuration – Justin Iurman Jan 27 '15 at 11:40
  • http://temp.example.com/section.php?id=6 is now http://temp.example.com/section/6 – ahmad ali Jan 27 '15 at 11:43
  • Yes, isn't it what you wanted ? – Justin Iurman Jan 27 '15 at 11:47
  • yes exactly . but is it normal, that i am getting 404 error now ? – ahmad ali Jan 27 '15 at 11:48
  • Since first rule is working correctly, second should too. So, no, this is not normal. Do you have other rules in your htaccess ? – Justin Iurman Jan 27 '15 at 11:51
  • No i have nothing else in htaccess – ahmad ali Jan 27 '15 at 11:54
  • I just read your update. That's because you don't use my second rule (the last line in my code is mandatory too) – Justin Iurman Jan 27 '15 at 12:00
  • I am using both now . but still getting 404 – ahmad ali Jan 27 '15 at 12:03
  • Definetely weird. Do you have access to error logs ? – Justin Iurman Jan 27 '15 at 12:04
  • Yes and that is clean. no error. . . actually am working in a subdomain. . does it has to effect anything ? – ahmad ali Jan 27 '15 at 12:08
  • It could. What is the root folder of your subdomain ? Is it the same as main domain ? – Justin Iurman Jan 27 '15 at 12:10
  • What if you replace last line by this one: `RewriteRule ^section/([0-9]+)$ /temp/section.php?id=$1 [L]` – Justin Iurman Jan 27 '15 at 12:15
  • Thank Justin. . no need to change anything. i just moved my code to root and its working fine. . thanks – ahmad ali Jan 27 '15 at 12:30
  • That's what i said in my answer (`which has to be in root folder`). That's why second rule wasn't working. Glad your problem is solved, you're welcome ! – Justin Iurman Jan 27 '15 at 12:34
  • Options -MultiViews RewriteEngine On # redirect "/section.php?id=xxx" to "/section/xxx" RewriteCond %{THE_REQUEST} \s/section\.php\?id=([0-9]+)\s [NC] RewriteRule ^temp/section/%1? [R=301,L] # internally rewrite "/section/xxx" to "/section.php?id=xxx" RewriteRule ^temp/section/([0-9]+)$ /section.php?id=$1 [L] this works for subdomain too now thanks again :) – ahmad ali Jan 27 '15 at 12:35
  • is it possible to change 6 to example-title in url like example.com/section/6 to example.com/section/travel id=6 has a title name travel in database – ahmad ali Jan 27 '15 at 12:40
  • Sure but you should post another question instead, since it would be a bit different from your initial question here – Justin Iurman Jan 27 '15 at 12:45
  • thank u justin . can you please help me with this http://stackoverflow.com/questions/28171065/hot-replace-id-with-associated-db-title-in-url-using-htaccess – ahmad ali Jan 27 '15 at 12:56
  • @JustinIurman Can you help me with this http://stackoverflow.com/questions/30242996/url-rewriting-and-url-redirection – Roxx May 15 '15 at 09:00
6
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^section/([^/]+)$ /section.php?id=$1 [L]

This will turn example.com/section.php?id=X to example.com/section/X

I suggest storing the URI in a database then using section.php?uri=

For example:

example.com/section.php?uri=super-awesome

would turn into:

example.com/section/super-awesome

Axiom
  • 902
  • 1
  • 10
  • 23