How to rewriting URL using database in php - php

I have little knowledge of .htaccess file and able to rewrite url. but for huge number of pages and page specific url requirements, i can't define this in .htaccess file. I had to use database for storing different urls of different pages, now i don't have any idea how to use it. I know working of dynamic url rewriting also like rewriting following url
www.mysite.com/garments/news.php?id=32
to
www.mysite.com/garments/news/32
but what about random page urls like
www.mysite.com/garments/news.php
to
www.mysite.com/latest-news

To get this link i would use this:
RewriteEngine On
RewriteRule ^latest-news/([0-9]+)/?$ garments/news.php?id=$1
What this does is redirect this url:
www.mysite.com/latest-news/30/
to this:
www.mysite.com/garments/news.php?id=30

Related

How to generate custom url in php? (Not url shortner)

I am creating a website where people can see each others profiles and I want to open their profile with specific links like https://www.example.com/johnsmith.
johnsmith is the custom URL. How can I generate the custom URLs in PHP like https://www.example.com/abc? In this case ‍‍‍/abc is the custom one. How can I create this custom URL via PHP or JavaScript? And yes I am not talking about a URL shortener!
It is something which is handled by your WebServer. e.g. you have this url:
http://example.com/?action=abc
then you will rewrite it like this
http://example.com/abc
In apache you can achieve so with using this code in your .htaccess file:
RewriteRule ^([a-zA-Z_.0-9]+) ?action=$1
The code says your url should start with any number or string but we don't know about the ending. you can specify the ending by adding a $ at the end of your Regex.
I wonder if you know how to work with htaccess or not. Anyway it is something totally different and can not be explained in just a sentence. Just a tutorial on htaccess
htaccess
You need to 'rewrite' URL's on the server-side. Like in Apache or nGinx. You can't just do it with PHP or JavaScript.
For example in nGinx:
rewrite ^(/willsmith/.*)$ /mypath/pages/willsmith.html last;

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.

Rewriting php urls to produce clean urls .htaccess

I'm using a custom built CMS and I need to rewrite the urls because currently they take the page ID and name of the page into the url.
/pages/1411760825/Facilities is an example of the current url
/Facilities is what I want it to show up as in the URL bar
The pages are linking to each other based on their ids and titles and I want it to not do that anymore.
This is the current .htaccess rule
RewriteEngine on
RewriteRule (.*)\.xml(.*) $1.php$2 [nocase]
I know I need to add a line below to rewrite what I want it to be, but I'm unsure how to achieve this.

Designing an url for a specific page while using php to load different content

When designing a website what would be good practice for the url designing and related backend.
Say I have a website with news posts on it. I would like the url to look like this:
mysite.com/news/newsItem1
Normally an url like this would reference to a folder called newsItem1 on the server. But in this case there will be a lot of newsItems (stored as xml files), and I don't think it's a good idea to create a folder for every item.
The same thing happens when I make the url like this:
mysite.com/news/newsItem1.html
In that case I would have to make a html file for every newsItem.
What I would like is an url that is different for every newsItem, but on the server it basically points to a php file that parses the matching xml file and returns its contents. How would I do that?
For this you have to use .htacces which allows you to redesign the url.
you can use something like this for that you want :
http://www.mysite.com/news/newsItem.php?id=1
and write the rewrite rule for url design in .htaccess file as:
RewriteEngine On
RewriteRule ^id/([^/]*)\.html$ /news/newsitem.php?id=$1 [L]
and the result will be as:
http://www.mysite.com/id/1.html
Here is the link for .htaccess tutorial
http://www.htaccess-guide.com/

Categories