9 Replies - 891 Views - Last Post: 10 July 2014 - 12:13 PM

#1 boba fett  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 70
  • Joined: 18-August 13

Using htaccess to re-direct & make URL look better

Posted 07 July 2014 - 01:27 PM

(1) Firstly, I have my site set up with one page "index.php" in the root directory handling all other pages. For example; my about page is "index.php?page=about". But I want users to be able to get directly to this page by typing in "mywebsite.com/about" in the address bar of their browser. I heard you can do this with htaccess, but I'm not very good with that yet. So, right now I have an index.php file in a folder that is named for each web page I have.

For example; I have a folder named about in the the root directory that has one file in it "index.php", and in that is one line that re-directs the page back to the "root index.php" like this >
header('Location: ../index.php?page=about');


That's working fine but I want to do it with htaccess and want to know if I should put an htaccess file in each folder or just have one in the root and what should it have in it and if I can delete those folders. Also, do I need to have a redirect for each page?

(2) I also want to change what a user sees in the address box of their browser when they go to one of the pages of my site from "mywebsite.com/index.php?page=about" to "mywebsite.com/about ,for example.

I've been reading up on some stuff I found on-line. So far I turned mod-rewrite on in Apache, made an htaccess file and this is what's in it>
Redirect /about/ http://mywebsite.com/index.php?page=about
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]


I don't know if that's right and do I only need one in the root directory or do I need one in each folder? I took out the index.php file in the about folder and tried the above in both the root and the about folder and neither works. I'm not re-directed and when I type in index.php?page=about in the address box I go to the about page but the URL looks like what I typed and not "root/about".

This post has been edited by boba fett: 07 July 2014 - 01:30 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Using htaccess to re-direct & make URL look better

#2 boba fett  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 70
  • Joined: 18-August 13

Re: Using htaccess to re-direct & make URL look better

Posted 07 July 2014 - 03:20 PM

Ok, I solved the first problem on my own.

I just needed this in one htaccess file in the root directory with the following commands.

DirectoryIndex index.php
Redirect /about/ http://localhost/index.php?page=about
Redirect /contact/ http://localhost/index.php?page=contact


One makes sure that the "index.php" in the root directory handles everything and the others are for individual pages.

Now, to work on my 2nd problem. :)/>

This post has been edited by boba fett: 07 July 2014 - 03:21 PM

Was This Post Helpful? 0
  • +
  • -

#3 boba fett  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 70
  • Joined: 18-August 13

Re: Using htaccess to re-direct & make URL look better

Posted 07 July 2014 - 03:59 PM

Well, I tried to make the part that makes the URLs look better and this is what I came up with:
Options +FollowSymLinks -MultiViews
<IfModule mod_rewrite.c>
	RewriteEngine On 
	RewriteCond %{SCRIPT_FILENAME} !-d
	RewriteCond %{SCRIPT_FILENAME} !-f 
	RewriteRule ^([0-9a-zA-Z\-]+)$ index.php?page=$1 
</IfModule>

But, it's not working. LOL

I re-wrote and updated

This post has been edited by boba fett: 08 July 2014 - 10:44 AM

Was This Post Helpful? 0
  • +
  • -

#4 boba fett  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 70
  • Joined: 18-August 13

Re: Using htaccess to re-direct & make URL look better

Posted 08 July 2014 - 11:27 AM

I now get this error: "This webpage has a redirect loop" from the browser.

This post has been edited by boba fett: 08 July 2014 - 11:27 AM

Was This Post Helpful? 0
  • +
  • -

#5 ArtificialSoldier  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1354
  • View blog
  • Posts: 4,667
  • Joined: 15-January 14

Re: Using htaccess to re-direct & make URL look better

Posted 08 July 2014 - 12:06 PM

I don't think that's an issue with .htaccess necessarily, since .htaccess is not telling the browser to redirect. The index.php page probably has a redirect that keeps going to itself.
Was This Post Helpful? 1
  • +
  • -

#6 boba fett  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 70
  • Joined: 18-August 13

Re: Using htaccess to re-direct & make URL look better

Posted 09 July 2014 - 12:04 PM

View PostArtificialSoldier, on 08 July 2014 - 07:06 PM, said:

I don't think that's an issue with .htaccess necessarily, since .htaccess is not telling the browser to redirect. The index.php page probably has a redirect that keeps going to itself.


Never had a problem, until I messed with the htaccess file. But I fixed it and it's back to normal now. Normal as the URL looks like [email protected] again, but at least no more errors LOL
Was This Post Helpful? 0
  • +
  • -

#7 ArtificialSoldier  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1354
  • View blog
  • Posts: 4,667
  • Joined: 15-January 14

Re: Using htaccess to re-direct & make URL look better

Posted 09 July 2014 - 12:26 PM

Quote

Never had a problem, until I messed with the htaccess file.

I understand that, but the htaccess file does not contain a redirect. The redirect is not happening there, it is happening inside index.php. If you fix the source of the problem then you can use URL rewriting.
Was This Post Helpful? 1
  • +
  • -

#8 boba fett  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 70
  • Joined: 18-August 13

Re: Using htaccess to re-direct & make URL look better

Posted 10 July 2014 - 06:17 AM

View PostArtificialSoldier, on 09 July 2014 - 07:26 PM, said:

Quote

Never had a problem, until I messed with the htaccess file.

I understand that, but the htaccess file does not contain a redirect. The redirect is not happening there, it is happening inside index.php. If you fix the source of the problem then you can use URL rewriting.


Ok, actually their is a loop, but its in the htacess:
# set default file to open
DirectoryIndex index.php site-down.htm
# one of these for each page I have
Redirect /about/ http://localhost/index.php?page=about
# new and improved URL re-write
RewriteEngine On
RewriteRule ^([^/]*)$ /index.php?page=$1 [L]


I know the re-write code is right because I got it from this site which does it for you>Generate It!

My gues is that I need to adjust the php.ini becuase now I get this Error>

Quote

Internal Server Error

The server encountered an internal error or misconfiguration and was unable to complete your request.

Please contact the server administrator at [email protected] to inform them of the time this error occurred, and the actions you performed just before this error.

More information about this error may be available in the server error log.


I receive this error on my computer using localhost and when I upload to my host server.
Was This Post Helpful? 0
  • +
  • -

#9 ArtificialSoldier  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1354
  • View blog
  • Posts: 4,667
  • Joined: 15-January 14

Re: Using htaccess to re-direct & make URL look better

Posted 10 July 2014 - 09:09 AM

Oh right, I missed that redirect line in the first post.

Well, do you need the redirect? Instead of redirecting /about/ to index.php?page=about, why not rewrite the URL so that index.php serves requests for /about/? Wouldn't the rewrite rules do that also?
Was This Post Helpful? 1
  • +
  • -

#10 boba fett  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 70
  • Joined: 18-August 13

Re: Using htaccess to re-direct & make URL look better

Posted 10 July 2014 - 12:13 PM

View PostArtificialSoldier, on 10 July 2014 - 04:09 PM, said:

Oh right, I missed that redirect line in the first post.

Well, do you need the redirect? Instead of redirecting /about/ to index.php?page=about, why not rewrite the URL so that index.php serves requests for /about/? Wouldn't the rewrite rules do that also?


Hmm, that could work. But, I took out everything but the re-write rule and even my home page has the error. I don't think I can re-write URLs with htaccess with the way mine and my host's appache/php are configured. So, yea, have to find out some work around to change the URL, maybe with PHP?

This post has been edited by boba fett: 10 July 2014 - 12:16 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1