Jump to content

php and friendly urls help please


Go to solution Solved by PNewCode,

Recommended Posts

Hello
I've tried many many many different ways of making a rewrite htaccess file for this and every time I've come up with getting a 500 error. Even when used a bunch of the different generators to make them online.
However even the generators don't really produce what I would want anyways, though I was using them to at least get a good starting point to figure it out. However no htaccess file will work as I keep getting the 500 error regardless

This is what I'm trying to do. 

1: There is a form on my website where people can type a single word with no spaces (and is forced lower case) to have a custom url
2: I want to make it so this url is what pulls up their page

This link currently works

https://www.examplewebsite.com/profile.php?id=johndoe (not a real link but for testing it works)

And I am trying to get it to be so he can share his link to people just using

https://www.examplewebsite.com/johndoe (much like you see for facebook pages and other friendly url website links)

Alas, after 3 weeks and dried up eyes, I have had no luck at all.

Is there a way to do this with just PHP? and if not, then is there a way to do this another way other than the htaccess rewrite file?

Many thanks

Note: I didn't include any of the htaccess codes that I tried because I tried so many of them that I don't even know which one would have been the closest to correct since they all gave me the same error

Link to comment
Share on other sites

PHP is the cart, your web server is the horse. You cannot put the cart before the horse.
Pick the simplest .htaccess you've tried and post it here, then we'll see what's wrong with it.

Can I also assume that you, at some point during these 500 errors, looked in your server's error logs for an error message? What did it say?

  • Like 1
Link to comment
Share on other sites

Posted (edited)

@requinix sorry I forgot to mention that. Yes I looked in the logs and the only error is how the htaccess file is affecting other pages with "Option FollowSymLinks not allowed here" but I'll tackle that headache later. I might have to do all of this in a different folder with its own htaccess to avoid all of that (example www.website.com/artist/john...)

This is the newest one I have. No errors for it's own page but it's returning my own connection error I made when it can't find the specific name in the database so

https://www.examplewebsite.com/profile.php?id=johndoe

but

https://www.examplewebsite.com/johndoe

now just says "Which profile are you trying to see" (from not being able to grab the "custurl" value in the database)

(side note: custurl is the row in the database where the value is entered for the chosen friendly url, in this case it being johndoe)

ReWriteEngine on

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

 

Edited by PNewCode
Link to comment
Share on other sites

UPDATE: I changed the setting in PLESK that I found for the error so that's not an issue anymore. Now it's just a matter of it not working because the page isn't finding the chosen URL value extension

Link to comment
Share on other sites

  • Solution

UPDATE:
The following works. I'm posting it here in case it helps anyone else. I have to admit though that I don't understand WHY it works and I am going to dig deeper in this so I can understand. I give credit to @requinix because that reply did spark a few flares in this old noggin on some things to search for too

Working htaccess code
 

<IfModule mod_rewrite.c>
    RewriteEngine On

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule (.*) /profile.php?id=$1 [L]

</IfModule>

 

Link to comment
Share on other sites

Well, look at the difference between the two (besides the IfModule).

Before you had

RewriteRule ^(.*)$ profile.php?$1 [L,QSA]

That would turn "johndoe" into "profile.php?johndoe". That's not what you wanted.

Now you have

RewriteRule (.*) /profile.php?id=$1 [L]

That will produce "profile.php?id=johndoe".

Keep in mind this will kick in for any path that doesn't exist - not just "words". You should consider limiting exactly what this can match.

  • Like 1
Link to comment
Share on other sites

@requinix Indeed! I spent a good part of the day digging in to understand it more, though I'm not totally getting it in my head yet but I understand a lot more now.
Luckily, these profiles are on a completely seperate part of the website so the htaccess file only applies to that folder, and there's a different domain name that points to it. So it serves it's purpose. However as you said, if I restructure that then I'll have to revisit how I path the pages. Might end up having to use it by id's only

Link to comment
Share on other sites

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.