How Google URLs format applied to php application? - php

In many php websites URLs is written as page.php?id=123 or rewrite moded page/Id/123
But is google I noticed that URLs is like google.com/search?q=Wordpress
I try to format website links to be similar but I didn't succeed even with rewrite rules
Also I found websites use same URLs formatting
Torrentz2.eu
How to apply this type of links in php application?

You need to save slug for each record.
for example
title of a product: A4Tech Mouse
slug : a4tec-mouse
so you can set url
example.com/searc?q=a4tech-mouse
and set htaccess rule for above url

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;

Website URL rework / reformat to user-friendly

I need some advice on where and how I should start this project. The current site is running classic ASP but uses .php extensions in the file system (don't ask - previous freelance).
Current URL structure has sub folders that contains display pages that renders the page
team/team_detail.php?teamID=3&Source=Title&Title=Partner
Only the teamID part actually does anything and query the data in a team table.
I was thinking of using the IIS url rule and add user friendly rule for each matching URL patterns. I was planning to change all website code to use new structure:
url team/team_detail/3/bob-titans
Old url formant that will still work team/team_detail.php?teamID=3&name=bob-titans
Would this complete site wide URL change be the most effective way to do this or would there be another more effective method?
If your main objective is to change the URL structure of the site (and clean up stuff like the unused "Source" and "Title" parameters while you're at it), then the approach is sound:
Modify the site code to use the new URL strategy (for example, with ASP.NET Routing).
Use the IIS URL Rewrite Module to create rules to properly redirect or rewrite the old-style requests to the new format. If this is a public site, and you are concerned about SEO, you should consider making these redirects use the 301 response type to tell search engines the resources under the old URLs have moved permanently to the new URLs.
It's a nice approach because you can focus on updating your code for the new structure, and let IIS take care of any users still referencing the old URL formats.

Reading Variables from SEO Friendly URLS

I'm working on a custom theme template for Word Press and I was wondering how to read the variables from the SEO friendly URLS. The website admin can choose between standard URLS http://yourdomain.com/?p=10, which is easy enough to read from with $_GET['p']. But how do I go about reading from the SEO friendly urls http://yourdomain.com/your-page-name/.
Thanks.
Are you on the page http://yourdomain.com/your-page-name/ trying to get the $_GET['p'] value? Just call $_GET['p'] it should work and should have a value of your-page-name. To my understanding mod rewrite only masks the look of the address for the browser bar, but the url is on server side still handled the same way.
Or are you on a different page and you are examining the link http://yourdomain.com/your-page-name/ trying to figure out how to get your-page-name as a variable? For that I would suggest you do either a string split, string position, explode, or regular expression... Many options I think.

Redirect url for public page in wordpress?

I am working on this site developed in Wordpress 3.3.1. My client wanted me to develop a public page. This page will be a simple php page. It will reside in wordpress directory but will not be a part of CMS itself. The issue here is that I want a url redirection for this page. My client wants to send a link in email to members of the website, so he wants to keep the url clean.
This is the format of current url that he wants to send to the members:
'http://www.example.com/shop/"url_encoded_category_name"/product/"product_id"'
And I want to redirect it to:
'http://www.example.com/template-public-home?productId="product_id"'
I was rewriting the url for now. But as you can see in the first url format that "url_encoded_category_name" and "product_id" are variables and therefor rewriting would mean that I am trying to rewrite different urls to same url(only query string changes). I want to change it to redirection because as I understand this approach of rewriting multiple urls to the same url is penalized by most of the search engines.
For rewriting I edited the .htaccess file.
What I wanted to ask is that considering that I have a publicly accessible page within wordpress directory:
what is the most suitable way to redirect my url?
In my case what is a better Code? 301 or 302?
Thanks for any assistance you can provide or direct me to a source where I could learn about it.
First of all, if your client just wants to use the generated URL in emails to members, no search engine will ever know. However, if there is a chance that they leak you might indeed end up with duplicate content. Redirection then is the correct approach.
You can redirect just as you rewrite by using the [R] flag in your rule (usually in conjunction with L as [R,L] so that the rules below that match are not executed.
From the point of view of a search engine (and a user as well), these are permanent redirects - you will never ever use the URL in the email as a primary URL (or will you?). That means you should use R=301.
Take a look at the documentation to learn about the flags, test your rewrite rules online here and check https://stackoverflow.com/questions/1426056/good-htaccess-mod-rewrite-url-rewriting-tutorial for some hands-on material.

Very simple mod_rewrite questions

1- Does mod_rewrite means that if I make this url:
domain.com/ad.php?id=8498292
INTO
domain.com/8498292
that all links on my website will have to be changed to the later above?
example the link: domain.com/ad.php?id=8498292 wont work now, unless I replace it with domain.com/8498292 ?
Or will the server know that they are the same still?
2- Will the rewritten link appear rewritten in the browsers adress bars also, so if I enter domain.com/ad.php?id=8498292 it will actually appear as domain.com/8498292 in the adress bar itself?
3- Will images and all other related links and material on the page whose link is rewritten remain intact? ie will pictures and links still work FROM that page which are relative?
Thanks
You can write the rules such that both will work, but generally you'll want the links to be in the "clean" format for when search engines index your pages.
mod_rewrite can do an internal rewrite if the pages are on the same domain. One would have to use the [R] flag to force an external redirect if that was desired.
You can make the rules as expansive or as restricted as necessary in order to avoid rewriting media URLs. RewriteCond has a number of ways to test the viability of rewriting.

Categories