PHP - User Profile URL From Ugly URL - php

Alright, I usually try to figure things out on my own, but I am stumbled. I am currently making a PHP registration/login/profile system with PHP and SQL. I am 97% finished, but I would like some help with the users' profiles.
I currently have a form where you can search for users. This is a GET form that links to "www.example.com/profiles.php?username=." I would like for it so that I can link it to something like "www.example.com/user/" or "www.example.com/profile/." I would also like the functionality of going directly to the "user/" URL.
I am sure there are rewrite rules to do this, and I have tried many. I still seem to have trouble getting it to work. Any help? Thanks in advance.
Here is my current .htaccess file (doesn't work):
RewriteEngine On
RewriteRule ^user/([^/]*)\.php$ /profile.php?username=$1 [L]
If this htaccess does work, what should I be linking to? Because currently, linking to the "/profile.php?username=" doesn't do anything.
Again, to clarify, I want the output from the GET form to be able to link to www.example.com/profile/USERNAME or www.example.com/user/USERNAME. I would also like for the user to access their profile by typing www.example.com/profile/USERNAME.

Related

How do I change the url look with .htaccess but keep the php functionality?

I'm trying to learn, so please be kind. My site is database driven and uses ID numbers to generate the pages. I'd like the URL not to show the ID number but the name of the page. Is there a way to do this where it is just cosmetic and doesn't effect the site? I'm also most likely screwing up the way I'm writing the redirect as it doesn't seem to work at all? What am I doing wrong? Thank you.
This is how my URL looks now:
http://mydomainname.com/index.php?id=35-Entertainment
I'd like it to look like this:
http://mydomainname.com/Entertainment
RewriteEngine On
RewriteRule ^([^/d]+)/?$ index.php?id=$1 [L,QSA]
Unfortunate there is not a way to do this.
The process in this case is the following:
A user enters the a url. In this case you want a nicely formatted URL (http://mydomainname.com/Entertainment)
The user sends this url to your server
Apache processes the url and points it to your project directory based on the domain used in the url
There Apache uses the htaccess to see what to do next. In this case the best method would most likely be to use RewriteRule ^(.*)$ index.php?url=$1 [QSA,L] or something like that
Then you need to do the routing within your app. You can use a routing table for instance to map the nicely formatted slug to an module/id combination
This process can't be done by Apache for it does not known about the content present in your database.
Hopefully this is of any help for you.

htaccess pretty urls redirects the wrong way

I've look up for the answer to this question all around the web but I didn't find anyone with the same issue I have.
I want to create a sort of track-order like amazon or like any carrier does. To do that I assign a unique track-number to orders. Users can type the track-number into a form or directly into a url.
I get the track-number via the get method, so I need to prettify urls, but I also want to keep the search form functionality so I need a redirect.
I tried in this way:
the original url: /order/?reference=track-number where track_number is a random token like TRG58a8a132ec858.
the desired url: /order/track-number as stated above track_number is a random token like TRG58a8a132ec858.
in my .htaccess I wrote this rule:
Rewrite Engine on
RewriteRule ^/?order/([A-Z]{3}.*)/?$ /order/?reference=$1 [L,R=301]
I got some inspiration here, here, and here.
It works on reverse: when I type the original-url it does not redirect, when I type the desired-url instead it redirects to the original-url.
I am a little bit confused. Where I am making mistakes?

htaccess change requested filename

I need to do this using htaccess
When a request is made for http://www.example.com/home, it should (internally)load the page http://www.example.com/home_st.php
Similarly when a request is made to other link called products (http://www.example.com/products), it should (internally)load http://www.example.com/products_st.php
In short what it is doing is appending "_st.php" and loading that URL. But one thing I do not want is if the user directly types http://www.example.com/home_st.php or http://www.example.com/products_st.php in the browser, it should show 404 / Page not found error
I have few other pages in that folder and I want those pages to behave in this manner. I understand the htaccess should have something like this
Turn on the rewrite
Forbid access if the URL is called with page names like home_st.php, products_st.php etc.
If it's "home" or "products", then rewrite(append?) it to home_st.php and products_st.php respectively. I have other files too while need to follow the same
P.N: My URL should not show the actual filename, for example home_st.php, products_st.php etc. It should only show as http://www.example.com/home, http://www.example.com/products etc
htaccess and regex is not something that I am well acquainted with. Any help would be great. Thanks
You want to be able to re-write URL's
This has been written before but i'll say it again.
You want to use the Htaccess file and the re-write rule.
RewriteEngine On # Turn on the rewriting engine
RewriteRule ^pet-care/?$ pet_care_info_01_02_2008.php [NC,L] # Handle requests for "pet-care"
This will make this url: http://www.pets.com/pet_care_info_07_07_2008.php
Look like this: http://www.pets.com/pet-care/
Links to more information: How to make Clean URLs
and for the webpage I used to reference this information from: https://www.addedbytes.com/articles/for-beginners/url-rewriting-for-beginners/
To get your pages set the way you want them, I would advise that you read the two articals and try get what you are looking for.
If you need some specific help with doing this, ask.
Hope that helps.

Using mod_rewrite for user pages

So for now, when you use my search function to search a user and go to their page, you will get something like this. "http://www.mywebsite.com/profile/?pid=username"
The php on that page then gets the GET variable, and runs a query to get all of the information needed.
I would love to get rid of the ?pid= part and have just "http://www.mywebsite.com/profile/username"
But I am having trouble understanding any of the results I get with google on this specific URL. Most examples involve PHP files, which I'm using wordpress and embedding my php via execphp.
This should work for you in a .htaccess file:
RewriteEngine On
RewriteRule ^/profile/([^/]*)$ /profile/?pid=$1 [L]

PHP id in .htaccess

I Have a .htaccess file for a blog script which currently generates a number for a blog post based on it's ID in the MySQL database. Is there any way I can change this so that it uses something like $subject as the URL from my PHP file? I have pasted my current .htaccess code below.
RewriteEngine On
RewriteRule ^blog-([0-9]+).html$ index.php?act=blog&id=$1
I hope people can understand what I am trying to describe.
Thanks in advance.
Regards,
Callum Whyte
Using just the subject is probably a bad idea, if you ever change the title, all links to your page (from google, or elswhere) will be broken. I would include both the subject and the idea, that way you'll always be able to match an URL to a blog post:
RewriteEngine On RewriteRule ^blog-[^-]+-([0-9]+).html$ index.php?act=blog&id=$1
This should match everything except a - between blog and the id.
Now you should be able to call, e.g., *yousite/blog-some_title_or_other-494.html* and it will call index.php?act=blog&id=494. This new rule shouldn't break any of your old backlinks either. If it does, let me know and I'll fix it.

Categories