• 0

.htaccess and RewriteRules [Solved]


Question

Hi,

On the latest site I am working on I am using a single php file, which loads different data from the database based on the content of the $_GET['page'] tag.

Now obviously this results in URLs such as mysite.com/index.php?page=Contact-Us which is rather unattractive and not very efficient for search engines.

So what I would like is mysite.com/Contact-us to load the page mentioned above.

However if only life was that simple.

Contact-Us is simply one name of a page that exists right now, there are infact several and they will be constantly changing so the file will need to allow for this.

And just to make things even more complicated there is a directory called admin which needs to be accessible without the rewrite rule kicking in.

I have tried the following but it does not work, firstly the $_GET['page'] variable is index.php not Contact-Us.

Secondly it does not let mysite.com/admin access the admin directory.

Options +FollowSymLinks
RewriteEngine on

RewriteRule (.*) index.php?page=$1
RewriteRule admin admin [L]

Any help on this would be much appreciated.

Joe

Link to comment
Share on other sites

8 answers to this question

Recommended Posts

  • 0

Thanks for your help DeathLace!

I had to remove the / before index.php but that aspect now works.

My next issue is going to mysite.com/admin Is there anyway I can make it so that the domain admin is an exception to the rule. As all the admin tools are in a separate directory. I suppose I could host them on a different sub directory but that is not really what I want to happen.

Any advice would be great

Joe

Link to comment
Share on other sites

  • 0

I have the same problem with you, unfortunately I didn't know how to do it (maybe Cupcakes does?!?! :p)... anyways, what I did was put an extra if statement @ the top of my PHP file

if($_GET['page'] == "admin") then include a diff file :)

But not the best way, there must be an 'ideal' way of doing it with HTACCESS unfortunately my knowledge of it is limited, I just had this answer because I figured it out a few weeks ago :)

Link to comment
Share on other sites

  • 0

Sorry would have edited previous post but the edit button has gone.

Anyway I have managed to fix this issue. It may not be how you would have liked to do it but it works for me and I thought I should share my trick :)

RewriteEngine on

RewriteRule (.+)/page index.php?page=$1

This does admittedly mean instead of going to mysite.com/contact-Us you have to go to mysite.com/contact-Us/page, but that is no issue for me. It also lets you access any directory you want, I would imagine if you had a directory called page inside another directory it wouldn't work but I haven't tried that one.

Hope this helps any one else who is stuck.

Joe

Link to comment
Share on other sites

  • 0

I don't want to open a new topic for this, so I'm asking here, it's somewhat related. Does anyone know how can I block the access to a website in cPanel, and keep it only to myself? I'm designing a site, and I don't want to do it on my PC, but directly on the hosting server. Obviously, I don't want people to access it before it's done. So, any ideas? :) Thanks.

Link to comment
Share on other sites

  • 0

Hey,

If you go into Cpanel and there should be an option for password protecting a directory. Then you just need to select what you want it to protect ( in your case the web root)

Hope this helps!

Link to comment
Share on other sites

  • 0

I don't want to open a new topic for this, so I'm asking here, it's somewhat related. Does anyone know how can I block the access to a website in cPanel, and keep it only to myself? I'm designing a site, and I don't want to do it on my PC, but directly on the hosting server. Obviously, I don't want people to access it before it's done. So, any ideas? :) Thanks.

If you've got a static IP then this will work out just fine:

<Limit GET POST>
order deny,allow
deny from all
allow from 127.0.0.1
</Limit> 

Where 127.0.0.1 is your actual IP address. This will block access to all IPs but the ones that are allowed. You can add more by creating a new allow from line.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.