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

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;

Related

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.

How to rewriting URL using database in 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

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?

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/

Changing WordPress URL structure while maintaing the proper 301 redirects with mod_rewrite

I currently have a blog set up with WordPress using URLs like so:
www.domain.com/blog/?pid=384092817
This was before I knew anything about anything. There's a significant amount of content there now and I want to change my URL structure to:
www.domain.com/my-post-title-384092817
Is there a way to set up my .htaccess and mod rewrite so when users go to the first url they get a 301 redirect to the second URL? I know to change my WordPress address (URL) setting from www.domain.com/blog to www.domain.com and my permalink setting to /%postname%-%post_id%, but how do I tell the old URLs to redirect to the new ones?
Do you actually mean that when users go to the second URL, it will be rewritten to the first? That can be done with
RewriteRule /blog/.+-(\d+)$ /blog/?pid=$1
If you want to send 301 redirects from the old URLs to the new ones, then you can't really do that with an .htaccess file. The reason is that Apache's mod_rewrite doesn't have access to the title of your post, so it won't know what title to insert in the URL. You'd have to do that redirect with PHP.
EDIT: y'know what, actually that's not entirely true. You can do some pretty crazy things with the RewriteMap directive, such as instructing Apache to ask an arbitrary program to perform the rewriting, and in that way you could have, say, a PHP script that does some database work to figure out what the title is and rewrites the URL appropriately based on that. But that seems like a way overcomplicated solution and I wouldn't suggest doing it - it'll be much cleaner and easier to just send the redirect directly from PHP.
Depending on your WP version, you can just use the Permalink redirect plugin -- should do the trick for you within WordPress and without mod_rewrite. However, as of WordPress 2.3, a lot of this should work automatically. At that point, the only thing you should have to do is redirect all your /blog/... requests to the route, which you can do via mod_rewrite roughly like this:
RewriteRule ^blog(.*) $1 [QSA]

Categories