Jump to content

Removing .php in url with php get vars


N-Bomb(Nerd)

Recommended Posts

Hi,

 

I'm have my website and I'm trying to create links to use my php page called us.php, but I want to have neat pretty links for it.. much like this: http://www.example.com/us/neat-awesome-video

 

Obviously the variable there would be "neat-awesome-video" and I want that passed into us.php.. how could I accomplish this? Also, what if I wanted to pass in a second variable.. how would I go about that?

Link to comment
Share on other sites

You would use Mod-Rewrite. There is a whole board on this topic within this forum.

 

So to get to http://www.example.com/us/neat-awesome-video you must start with your original URL. This may look like: http://www.example.com/us.php?title=neat-awesome-video

 

Now you can enable mod-rewrite in a .htaccess file that you upload to the website document root and add the url rewrite rule.

 

# .htaccess file contents
# enable mod-rewrite
RewriteEngine On
RewriteBase /

# add rule for us.php
RewriteRule ^us/([a-z0-9-]+)$ us.php?title=$1 [L]

 

Now your url will work when it is seo friendly. This part

([a-z0-9-]+)

is called a regular expression. In this example it will only accept lowercase letters a-z and numbers, and hypens. Any other characters will fail and throw a 404 error so you must make sure that your parameters contain the correct characters defined in your rewrite rule.

 

If you want to add extra parameters lets say an id that is a numeric value. So, your non-rewritten url will look like http://www.example.com/us.php?title=neat-awesome-video&id=123 We can amment the rewrite rule to:

 

# add rule for us.php
RewriteRule ^us/([a-z0-9-]+)/([0-9]+)$ us.php?title=$1&id=$2 [L]

 

The second parameter (id) is captured and can only be a number so now your URL will look like http://www.example.com/us/neat-awesome-video/123

 

Link to comment
Share on other sites

I've been reading up and trying everything I know and I'm unable to get this to work correctly. I'm using 1and1 shared hosting and I know for a fact that they have mod_rewrite enabled, but I can't seem to get this to work at all. Here's what my .htaccess file looks like thus far:

AddHandler x-mapp-php5 .php
RewriteEngine On
RewriteBase /
RewriteRule ^us/([a-z0-9-]+)$ us.php?title=$1 [L]

 

When I try to access the page via http://example.com/us/awesome-stuff I get a 404 error and the page doesn't load. My us.php file is located at http://example.com/us.php . Also, does the "www" prefix matter when dealing with these issues?

Link to comment
Share on other sites

Your script isn't causing the 404 is it? You must test the none rewritten url first:

 

http://example.com/us.php?title=awesome-suff

 

 

The www will not cause an issue but as a rule of thumb you should always redirect the non www version to the www version or vica-versa. Search engines used to see this sort of thing as duplicate content when both www and non www urls serve up the same page when people link to your website using both versions.

Link to comment
Share on other sites

Your script isn't causing the 404 is it? You must test the none rewritten url first:

 

http://example.com/us.php?title=awesome-suff

 

 

The www will not cause an issue but as a rule of thumb you should always redirect the non www version to the www version or vica-versa. Search engines used to see this sort of thing as duplicate content when both www and non www urls serve up the same page when people link to your website using both versions.

 

When using the url ( http://example.com/us.php?title=awesome-suff ) to test with it works perfectly.. that's why this is very frustrating to me.

Link to comment
Share on other sites

Try adding symlinks into the .htaccess

Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^us/([a-z0-9-]+)$ us.php?title=$1 [L]

 

Please take the time to carefully read this tutorial.

 

http://www.easymodrewrite.com/

 

I've already tried adding "Options +FollowSymLinks", but I just tried adding it again and I'm not having any success. I think there may have something to do with my shared hosting company and the "base" option, but I cannot figure that out.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.