1

I previously changed my .htaccess file so that my 'subdirectory' (13/) can be the 'directory' I use for my main domain by adding :

RewriteCond %{HTTP_HOST} ^(www.)?mydomain.com$
RewriteRule ^(/)?$ 13/index.php [L]

Now I want to remove the .php extensions and extensions like index.php?eventid=3 to index/something

EDIT#1[where something is the name of that id stored in the database]

How should I do that? Previously, I did work in some PHP-frameworks but they already have a dedicated url file. So I never thought much about it. But this time, I'm not using any framework, so I need to modify .htaccess.

I looked around, but was unable to find any reliable blog/sources.

EDIT #2

My sub-directory is public_html/13/.

ie, www.mydomain.com redirects to www.mydomain.com/13/ Now I want to access www.mydomain.com/13/events.php?eventid=1 as www.mydomain.com/events/44

xan
  • 4,640
  • 13
  • 50
  • 83

2 Answers2

1
RewriteRule ^([A-Za-z0-9\-]+)/([A-Za-z0-9\-]+)/([0-9]+)/?$ $1.php?$2=$3

The above code would redirect http://www.mydomain.com/index/eventid/3/ to http://www.mydomain.com/index.php?eventid=3

htaccess is very flexible, and creating variations of the above is pretty easy once you understand the syntax. You will want to read up on the regular expressions you can use, and the myriad of options the Rewrite module has to offer. Most of my redirects look like the above though, real simple.

EDIT: An example of what you're looking for is Wordpress, which uses slugs to identify pages based on something more semantic than an ID number. You would have to url encode the name of the item from the database and either store it in the database as the slug, or find some other way of making it usuable in the database query that retrieves the content of the page. Then your htaccess would look something like this:

RewriteRule ^([A-Za-z0-9\-]+)/([A-Za-z0-9\-]+)/?$ $1.php?slug=$2

In your PHP you will need to create slugs and store them in the db so they can be used as IDs in your queries.

EDIT2: Try changing your entire htaccess file to just this:

<IfModule mod_rewrite.c>
RewriteEngine on

RewriteCond %{HTTP_HOST} ^www\.yourdomain\.com$ [NC]
RewriteRule ^(.*)$ http://yourdomain.com/$1 [R=301,L]

RewriteRule ^([A-Za-z0-9\-]+)/([A-Za-z0-9\-]+)/([0-9]+)/?$ $1.php?$2=$3

</IfModule>

I have tested it and it works. Something else in your htaccess is messing it up.

Eric
  • 907
  • 7
  • 13
  • Ya. I worked with it in Python framework - Django. It was really simple in that one. But now, I'm doing it in PHP, so was having problems. I also wanted to know how can i change my `../index.php?eventid=3` part to `../index/thateventname`[where `thateventname` could be access from the database]. – xan Jan 21 '13 at 18:52
  • Your first code (without the slug one), is not working. IF I try `www.mydomain.com/events/eventid/8` instead of `www.mydomain.com/events.php?eventid=8`, it is not opening the page. – xan Jan 21 '13 at 19:07
  • That is because I forgot to add a question mark after the last trailing slash to make it optional. While flexible, htaccess is also strict. I edited my code above. – Eric Jan 21 '13 at 19:09
  • This post: http://stackoverflow.com/questions/8583856/how-to-vanity-url-ignore-php-extension-with-htaccess?rq=1 tells me why your htaccess isn't working, the first RewriteRule you have made is cancelling out all those beneath it. – Eric Jan 21 '13 at 19:29
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/23111/discussion-between-xan-and-eric) – xan Jan 21 '13 at 19:31
1

Just use the following rule:

 RewriteRule ^index/([0-9]+)/?$ /index.php?eventid=$1 [QSA,L]
Tommaso Belluzzo
  • 23,232
  • 8
  • 74
  • 98
  • Your code is not working. I tried this: `RewriteRule ^(www.)mydomain.com/events.php?eventid=$1 [QSA,L]` to open `www.mydomain.com/events.php/?eventid=5` – xan Jan 21 '13 at 19:11