Using mod_rewrite for user pages - php

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]

Related

Clean URL structure for php page without having a real page

I have a page where I am loading up a JSON file and matching data based on a users search.
The caveat however, is that I want to have really clean URLs for these results without actually making a new page for them. (For the life of me I don't know what the terminology for this is)
So when a user goes to website.com/names/adrian it will just land on /names/ and load the data based on "adrian".
You can do that with apache's rewrite rule:
RewriteEngine on
RewriteRule ^(names\/[a-z0-9A-Z-_]+)$ names.php?name=$1
Add this to your .htaccess file
It will send example.com/names/aName as get request to names.php.
And you can get that with $_GET["name"]; in names.php
By the way, you can see regex result in here: https://regexr.com/415mq

htaccess rewrite for multiple query string with one dynamic empty query string

So, I ran into an issue with query strings and rewriting them to seem "prettier"
My website is at: localhost/admin/ so when uploaded to my server it is also under the /admin directory, and the .htaccess file looks like this:
RewriteEngine On
# redirect URL with empty query string to index.php
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^/?(.*)$ /admin/index.php?$1 [NC,L]
Unfortunately, my design for the admin panel all of the pages are linked to the index.php file. So when I originally used to request http://localhost/admin/?users it would show a list of all of the users. I wanted to make this prettier so I used the above htaccess rules and came up with http://localhost/admin/users. Two issues arose with this:
The css files within the admin folder under css/ could not be accessed so none of the styles were being applied.
This ?users page contains pagination using the GET parameter in php as well, so it looks kind of like http://localhost/admin/users&page=1 and used to look like http://localhost/admin/?users&page=2.
So basically what I wanted to do was make the code look more like this: http://localhost/admin/users/page/1. Unfortunately I cannot hard code the users parameter in because I have other sections like "websites" and I will also have a edit users page with id's etc. So I was wondering if someone could help me with the best way to approach this problem or how I can fix the above htaccess rules? The only thing that kind of helped was: .htaccess mod_rewrite Unknown number of Variables of a GET form but I am still lost on how to implement it for multiple empty GET variables at the front of the url.
Thanks!
Here's what I do,
First for css part use <base href="domain.com/"> in head section of your pages.
You are redirecting full url to query string of index.php and I am assuming you are retrieving is as $_SERVER['QUERY_STRING'];.
So I suggest you that you change your pagination url to http://localhost/admin/users/page/1.
And after that when you retrieve them in index.php explode the string using explode function,
$arr = explode('/',$_SERVER['QUERY_STRING']);
Inside your $arr you will have all your values which you got them from query string.

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.

PHP - User Profile URL From Ugly URL

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.

I need suggestion for using a .htaccess in Code Igniter framework

My old website doesn't use a framework and the .htaccess worked fine, take a look at my .htaccess code below:
RewriteRule ^([a-zA-Z0-9_-]+)\.html solution.php?solution=$1 [L]
So the point there is that if it detects '.html' in the url, then the user is redirected to a PHP page passing any GET parameter.
Recently, I have converted my website to use the code igniter framework, so I have converted my .htaccess code to something below:
RewriteRule ^([a-zA-Z0-9_-]+)\.html index.php/solution/index/$1 [L]
I end up getting a page not found. However, if I still manually type
http://myhost/index.php/solution/index/xxx
then it works, but if I type
http://myhost/xxx.html
then it doesn't. My objective here is to maintain the original URL because it is indexed by Google and I do not want the previous visitors of my website to be getting a 'page not found' for my old links.
I need a fix or suggestion that might help. Looking forward to your response. Thanks.
Looks like you missed a lot of stuff while converting your website to use CI. Read about URL suffixes and also note you can use routes to redirect anything you can't handle normally using controllers.
You also need to modify your .htaccess to get rid of index.php since only modifying the config file won't work - there are a lot of resources about that, like this one.
Follow the codeigniter user guide: http://ellislab.com/codeigniter/user-guide/general/urls.html
Specially in following section:
Removing the index.php file
Adding a URL Suffix
If still having problem then check this wiki:
https://github.com/EllisLab/CodeIgniter/wiki/Removing-index.php-in-codeigniter-xampp-for-windows
or
How do I write a .htaccess file to make CodeIgniters URL routing work?

Categories