# .htaccess question



## The Infinity (May 12, 2005)

I was told a while ago that I could change my .htaccess to know when the URL said something like http://www.mysite.com/home.php that in the address bar it would automatically see it as http://www.mysite.com/home and it would display that in the address bar.

The person that told me this was a little unclear about how to do it. I was wondering if someone could give me a better explaination of how I can go about doing this.

- Thanks


----------



## knight_47 (Mar 15, 2006)

RewriteEngine On
RewriteRule ^*home*(/)?$ *home.php*
RewriteRule ^*video*(/)?$ *video.html*
RewriteRule ^*pictures*(/)?$ *pictures.htm*

Just add the above code to your .htaccess file, I have given 3 examples, all the bold are the parts that need to be edited out.

By the way, the reason (/) is in the RewriteRule is so you can have yoursite.com/pictures OR yoursite.com/picutres/ and they will both work.

Thanks namenotfound...


----------



## The Infinity (May 12, 2005)

Awesome. Will I need to add this for ever page I make?

Such as if I create a page called turtles.php will I need to go into the htaccess page and make the change:

RewriteEngine On
RewriteRule ^home(/)?$ home.php
RewriteRule ^video(/)?$ video.html
RewriteRule ^pictures(/)?$ pictures.htm
RewriteRule ^turtles(/)?$ turtles.php

?


----------



## watchintv (May 27, 2005)

I am also positive that there is another way to do it with out having to keep adding the name of the page to the htaccess page. I've done it before, but cant remember how you do it. Ill look it up.


----------



## watchintv (May 27, 2005)

Found it, I think.

Try adding just this


```
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.php [L,QSA]
```


----------



## namenotfound (Apr 30, 2005)

@watchintv that's cool I didn't know about that one :up:


The method I told Knight is good if you want relevant urls, such as if you have a site with articles and you want the urls to be dated. Such as:

site.com/November/27/2006/world-peace-starts

Instead of creating all those directories, you could do something like this:

RewriteRule ^November/27/2006/world-peace-starts(/)?$ worldpeace.php

The actual file would be at site.com/worldpeace.php but you'd see in the address bar site.com/November/27/2006/world-peace-starts


----------



## The Infinity (May 12, 2005)

Sorry if this is a bit of a dumb question since I'm really new to the .htaccess part. But all I have to do is add:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.php [L,QSA]

to my .htaccess and that should take care of my problem?


----------



## knight_47 (Mar 15, 2006)

The Infinity said:


> Sorry if this is a bit of a dumb question since I'm really new to the .htaccess part. But all I have to do is add:
> 
> RewriteEngine On
> RewriteCond %{REQUEST_FILENAME} !-f
> ...


Yup, it's that easy.

But I have a question, will this work if I have php, html, and htm files? Or do I need to modify the last line??


----------



## The Infinity (May 12, 2005)

knight_47 said:


> Yup, it's that easy.
> 
> But I have a question, will this work if I have php, html, and htm files? Or do I need to modify the last line??


Thats what I was wondering to because on the FTP server the files are called index.php with the .php ending.

Do I need to change anything, because I put it onto the .htaccess below everything else I have in that .htaccess file and it didn't work. I received a 404 error when I tried to take .php off the end in the address bar.


----------



## watchintv (May 27, 2005)

To be honest with ya, I have no clue how it works. I just had the site I saw it on bookmarked in firefox.


----------



## The Infinity (May 12, 2005)

watchintv said:


> To be honest with ya, I have no clue how it works. I just had the site I saw it on bookmarked in firefox.


Great


----------



## watchintv (May 27, 2005)

lol, im sure somebody knows.


----------



## The Infinity (May 12, 2005)

watchintv said:


> lol, im sure somebody knows.


I sure do hope so, I have been having this problem forever


----------



## namenotfound (Apr 30, 2005)

Forever? Your first post in this thread was at 12:34 AM today (EST)


----------



## The Infinity (May 12, 2005)

namenotfound said:


> Forever? Your first post in this thread was at 12:34 AM today (EST)


Lol, I have had the problem longer than that, I had a post way back and I had, had the problem a few months before that post. I just let it go until my members started to complain.


----------



## Eriksrocks (Aug 7, 2005)

Look online at Apache docs or for .htaccess tutorials. There are .htaccess syntax rules and there is a reason why it's working/not working!


----------



## The Infinity (May 12, 2005)

Actually I have looked online but since I'm not sure what the problem is called I can't pin point what I really need


----------



## knight_47 (Mar 15, 2006)

Where's ID10T when you need him...


----------



## namenotfound (Apr 30, 2005)

knight_47 said:


> Where's ID10T when you need him...


He's on this site too? That's cool


----------



## Eriksrocks (Aug 7, 2005)

Maybe I could help, but actually I'm a bit confused by what the problem is. Could you explain it again?


----------



## knight_47 (Mar 15, 2006)

namenotfound said:


> He's on this site too? That's cool


No he's not lol, but I wish he was, he's really good with .htaccess stuff.


----------



## watchintv (May 27, 2005)

Who is ID10T?


----------



## knight_47 (Mar 15, 2006)

watchintv said:


> Who is ID10T?


A member at another forum (www.xenweb.net) but he's an expert with .htaccess stuff, php, and really most of web development. He's an awesome guy.


----------



## The Infinity (May 12, 2005)

Eriksrocks said:


> Maybe I could help, but actually I'm a bit confused by what the problem is. Could you explain it again?


Ok pretty much I know I need to do something in my .htaccess file. And I need to figure out what I need to put in it to make my urls for the address bar say

http://www.mysite.com/main

instead of

http://www.mysite.com/main.php


----------



## Eriksrocks (Aug 7, 2005)

So if a user puts *either* /main or /main.php into their address bar, you want it to redirect to /main and then display the file /main.php but look like /main in the address bar?


----------



## Eriksrocks (Aug 7, 2005)

I'm not very good with .htaccess. I suggest you read this:
http://www.yourhtmlsource.com/sitemanagement/urlrewriting.html

I would, but I don't have time right now.


----------



## The Infinity (May 12, 2005)

Eriksrocks said:


> So if a user puts *either* /main or /main.php into their address bar, you want it to redirect to /main and then display the file /main.php but look like /main in the address bar?


YES  Eriks got it now


----------



## Eriksrocks (Aug 7, 2005)

What if you put main.php into the folder /main and renamed it index.php and then changed your .htaccess file to something like this:

RewriteEngine on
RewriteRule ^*main*\.php$ /*main*
RewriteRule ^/*main*/index\.php$ /*main*

The stuff in bold is what you change for each one. So if you wanted to add another group for pics.php then your whole .htaccess file would look like this:

RewriteEngine on
RewriteRule ^main\.php$ /main
RewriteRule ^/main/index\.php$ /main

RewriteRule ^*pics*\.php$ /*pics*
RewriteRule ^/*pics*/index\.php$ /*pics*

So the steps would be:

Create a folder called main.
Take main.php and move it into that folder. Then rename it index.php.
Then insert the appropriate stuff into your .htaccess file and make sure that the file is in your root directory. (www.yoursite.com/)

It's kind of a cheating workaround - it's not really a direct rewriting of the URL, but it works, and the user really won't notice much. 

Note: Tell me if it works - I'm just learning this stuff!


----------



## The Infinity (May 12, 2005)

Eriksrocks said:


> What if you put main.php into the folder /main and renamed it index.php and then changed your .htaccess file to something like this:
> 
> RewriteEngine on
> RewriteRule ^*main*\.php$ /*main*
> ...


Awsome, will give it a try when I get home. We just changed our website template so I'm working on the bugs we have so I'll try this then 

Erik we could use your expertise on my forums


----------



## Eriksrocks (Aug 7, 2005)

The Infinity said:


> Erik we could use your expertise on my forums


OK... 

Oooo... Nice new template.


----------



## The Infinity (May 12, 2005)

Thanks Erik 

I do have one question though. With this code does that mean I'm going to have to add the new pages to the .htaccess every time? If so maybe I'll just leave it the way it is since I have a lot of pages


----------



## Eriksrocks (Aug 7, 2005)

Yeah, unfortunately. I'm not good enough with .htaccess to find a solution that wouldn't require you to do that yet.


----------

