Apache htaccess rewrite using PHP script to change URL - php

I have urls that look like http://WEBSITE.com/?page_id=53?&nid=3104&lid=3100
As you can guess it is an ugly wordpress site, search function pulling up a result with id nid=3104 and lid=3100 . Both numbers are necessary.
I would rather URLs look like http://WEBSITE.com/Chicago-Shelter-name
or http://WEBSITE.com/shelter/Chicago-Shelter-name
How can I get an .htaccess file to read the search result from a PHP script (that will pull if from SQL using the NID and LID numbers) and when the name comes back as "Chicago Shelter name" rewrite the url http://WEBSITE.com/Chicago-Shelter-name

SOLVED
Not easy.
.htaccess needs:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^custom/name/?$ newfile.php?nid=3104&lid=3100 [NC,L]
</IfModule>
This being wordpress required a replica page of the entire search result. The menu and sidebar will not reflect any updates, but I had to break out of wordpress.
Copied the source of the search result, removed the result, added the PHP that displays the result.
All inside of "newfile.php"
The above .htaccess file works.
If http://WEBSITE.com/custom/name" is entered, it will display that entered URL
but in reality secretly display the result of "http://WEBSITE.com/newfile.php?nid=3104&lid=3100"
This is what I wanted. Not the best, and now I must create lines of code for every single possible item in search. But it works.

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.

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]

Website which generates page for any offset

I have a series of pages on my website which are generated identically using php and a database. The only thing that changes is which topic is selected from the database. I want the user to be able to navigate to the each of these topic pages directly like www.mywebsite.com/<TOPIC> where <TOPIC> is the name of ANY topic. So currently I have a source folder in my public html directory along with an index page for each of these topics. As I'm about to add a lot more "topics" I feel like there is a better way to do this.
What I am looking for is for the user to be able to type www.mywebsite.com/<TOPIC> and a SINGLE php script is used to generate a page based on what <TOPIC> is.
The only thing I could think of was using the 404.php page for this and only showing an error if the topic didn't exist. But with that idea, I don't know how I would get what <TOPIC> and feel like that is a hack.
create or edit your .htaccess file in your document root and write this:
...
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^([A-Za-z0-9-]+)/?$ index.php?page=$1 [NC,L]
</IfModule>
Then you can call a page like:
http://example.com/this-is-my-topic
and in your index you would be able to handle $_GET["page"] with the value of this-is-my-topic.
That is of course only if your server supports Clean URLs and mod_rewrite.c

URL rewriting with PHP MySQL .htaccess & GET

I have a MySQL database with 5000 items. The table is structured as
id
product-name
content
product-url
You can access a specific product page by going to www.site.com/products/index.php?id=123. The index.php file uses PHP to GET the ID to search the MySQL database and return the appropriate product name and content.
How can I make it so instead of www.site.com/products/index.php?id=123, the URL is rewritten or redirected to www.site.com/products/product-url? If I do it with .htaccess, will the index.php file still be able to know what ID to use to look up the product-url if the ID is not in the URL? Or is there some way to map URLs with PHP and MySQL?
(I'm a noob when it comes to this stuff, so any help will be much appreciated. Thanks.)
You can use .htaccess and a RewriteRule. The following will redirect an SEO URL to index.php with $_GET['product-url']. You will need to modify your code to either lookup the product id or use the product-url directly. Both should be unique in your products table.
RewriteEngine On
RewriteBase /
# redirect product page from SEO URL
RewriteRule ^products/([\w\d-_]+)/?$ products/index.php?product-url=$1 [L]
Note: I've made the product_url to only allow letters, numbers, dashes and underscores. I suggest such a restriction. In addition, the trailing slash is optional. Futhermore, this will not reappend a query string. If you need that you can add the QSA flag to the RewriteRule.

Categories