SEO friendly url using php
You can essentially do this 2 ways:
The .htaccess route with mod_rewrite
Add a file called .htaccess in your root folder, and add something like this:
RewriteEngine on
RewriteRule ^/Some-text-goes-here/([0-9]+)$ /picture.php?id=$1
This will tell Apache to enable mod_rewrite for this folder, and if it gets asked a URL matching the regular expression it rewrites it internally to what you want, without the end user seeing it. Easy, but inflexible, so if you need more power:
The PHP route
Put the following in your .htaccess instead:
FallbackResource index.php
This will tell it to run your index.php for all files it cannot normally find in your site. In there you can then for example:
$path = ltrim($_SERVER['REQUEST_URI'], '/'); // Trim leading slash(es)
$elements = explode('/', $path); // Split path on slashes
if(count($elements) == 0) // No path elements means home
ShowHomepage();
else switch(array_shift($elements)) // Pop off first item and switch
{
case 'Some-text-goes-here':
ShowPicture($elements); // passes rest of parameters to internal function
break;
case 'more':
...
default:
header('HTTP/1.1 404 Not Found');
Show404Error();
}
This is how big sites and CMS-systems do it, because it allows far more flexibility in parsing URLs, config and database dependent URLs etc. For sporadic usage the hardcoded rewrite rules in .htaccess will do fine though.
*****Copy this content from URL rewriting with PHP**
Create dynamic SEO friendly url in core PHP
Add this code in your .htaccess
RewriteEngine OnRewriteRule ^([a-zA-Z0-9-/]+).html$ product.php?pr_url=$1RewriteRule ^([a-zA-Z0-9-/]+)$ category.php?cat_url=$1
Create SEO Friendly URLs from _GET query
You have to redirect your orignal uri to the new uri , add the followng before your existing rule :
RewriteEngine on
RewriteCond %{THE_REQUEST} /(?:index\.php)?\?a=search&query=(.+)\sHTTP [NC]
RewriteRule ^ /search/%1? [NE,L,R]
PHP Seo Friendly URL
RewriteCond %{REQUEST_URI} ^download/(.*)$ [NC]
RewriteRule ^(.*)$ index.php?down=$1 [L,QSA]
to catch filename in index.php:
$filename = $_GET["down"];
Creating SEO friendly urls using htaccess
RewriteRule ^profile/([0-9]+)/([A-Za-z0-9-]+)/?$ index.php?p=profile&id=$1
Should work for :
www.domain.com/index.php?p=profile&id=20
to
www.domain.com/profile/20/profile-friendly-name
Make SEO Friendly URL in Php with help of .htaccess
With your shown samples, could you please try following. Please make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
##Rule for handling non-existing pages here to index.php file.
RewriteRule ^(A1-13-05-2021)/?$ index.php?title=$1 [QSA,NC,L]
Generic Rules: In case you want to make it Generic rules then try following rules only.
RewriteEngine ON
##Rule for handling non-existing pages here to index.php file.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(A1-\d{2}-\d{2}-\d{4})/?$ index.php?title=$1 [QSA,NC,L]
Seo Friendly URL results in CSS IMG and JS not working
You dont need the second rewrite rule. Your CSS/JS paths are all 'relative' to your current location.
Your CSS exists here:
/css/normalize.css
Your page is looking here:
/swift-details/2/abblinbb/css/normalize.css
All you need is 'forward-slashes' before your CSS/JS paths.
Rewrite Pretty or SEO friendly URL via htaccess
Let's understand RewriteRule
directive which is the real rewriting workhorse.
A RewriteRule
consists of three arguments separated by spaces. The arguments are
- Pattern: which incoming URLs should be affected by the rule;
- Substitution: where should the matching requests be sent;
- [flags]: options affecting the rewritten request.
Let's start with the following snippet:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
First line of code, describes RewriteEngine
is turned on. Here I use RewriteCond
directive which defines a rewrite rule condition. Using RewriteCond
directive we defined two conditions here. This directive took a server variable called REQUEST_FILENAME
. The two conditions above tell if the request is not a file or a directory, then meet the rule set by RewriteRule
. See more details on this issue.
Now it's time to write some rewrite rules. Let's convert
www.example.com/?pr=project-abc123
// to
www.example.com/project-abc123
and rewrite rule will be:
RewriteRule ^(.*)$ index.php/?pr=$1 [L]
And to get the www.example.com/pr/project-abc123
we need the rule as the below:
RewriteRule ^/?([a-z]+)/(.*)$ index.php/?$1=$2 [L]
// or
RewriteRule ^/?pr/(.*)$ index.php/?pr=$1 [L]
The [L] flag tells mod_rewrite to stop processing the rule set. This means that if the rule matches, no further rules will be processed.
SEO Friendly URL's with a /
First a disclaimer: no one will ever have a definitive answer of what is or isn't SEO friendly. That said, here's some hint.
The direct approach is to url_encode each part of the friendly URL. In your example, you can decide that an URL is defined as http://example.nl/tires/<vendor>/<measure>
so you url encode each part and obtain url like http://example.nl/tires/Afronden%20Landbouw/1000%2F50R25
. This are a bit more SEO friendly, but definetly not readable by users. This can penalize you when choosing between multiple results in SERP.
My suggestion is to replace each "page breaking" character with a dash (not underscores, since is a non-breaking character) and obtain a better url like http://example.nl/tires/Afronden-Landbouw/1000-50R25
.
Notice that I haven't placed the product ID in the URL; if you find a way of mapping URL to product without ID is much better, but if can't is not a big issue.
Then you need to think carefully about "categories". When defining sub-path, think at each level as an "explorable category", with nested product. This imply that valid URL could be:
http://example.nl/tires/
(search through all tires);http://example.nl/tires/Afronden-Landbouw/
(list of tires made by Afronden-Landbouw)http://example.nl/tires/Afronden-Landbouw/1000-50R25
(a single model)http://example.nl/tires/Afronden-Landbouw/1000-50R25/1601
(a single model, with explicit product ID)
With this organization you use a meaningful sub-path, search engines like Google, will have a better "understanding" of your websites and can provide better result to the user. Also, some advanced users can play with your sub-path and get meaningful resources.
Last and NOT least, always always always set redirect from the old URL to the new one. You can start with 302 redirect for testing, and then set a definitive 301 redirect. If you don't use redirect you will loose ALL your reputation so far... which is not SEO at all.
Happy SEO
Related Topics
Openssl Not Working on Windows, Errors 0X02001003 0X2006D080 0X0E064002
How to Make Apache Serve Index.PHP Instead of Index.Html
PHP Get Previous Array Element Knowing Current Array Key
Laravel - How to Get Current User in Appserviceprovider
Can't Execute PHP Script Using PHP Exec
How to Prevent PHP Files from Being Downloaded? and What Are Some Ways Someone Can Download Them
PHP Get the Last 3 Elements of an Associative Array While Preserving the Keys
How to Get Info on Sent PHP Curl Request
Magento - Load Only Configurable Products
What's the Difference Between Is_Null($Var) and ($Var === Null)
How to Install an Older Version of PHPunit Through Pear