Seo Friendly Url Using PHP

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]

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]

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.

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

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"];

PHP Convert String to SEO Friendly Url For Bengali Language Type

To accept glyph in Bengali (or any other language) you have to change the regex on this line :

 $string = preg_replace("/[^a-z0-9]/u", "$separator", $string);

Currently, it means "change any character wich in not a letter or a number by a -". By another regex asking "change any character wich is not a letter or a number in any language" :

$string = preg_replace("/[^\p{L}\p{M}]/u", "$separator", $string);

Changing this line, your function will work fine !
More information and related anwser here : https://stackoverflow.com/a/6005511/15282066

SEO Friendly URL

I'm not sure why people are being so deliberately obtuse here...

What you are looking for is mod_rewrite, an apache module for URL rewriting.

In your .htaccess file (you might need to make it) put:

<IfModule mod_rewrite.c>
RewriteEngine on

RewriteRule ^blog\/([0-9]+)\/.*$ /blog.php?post=$1 [L]
</IfModule>

This means when you go to /blog/10/any-old-bit-of-text/ behind the scenes it is identical to if you visited /blog.php?post=10.

The ([0-9]+) bit is called a regular expression (or regex), and matches any number. The .* means match anything. The ^ anchors to the start of the query and the $ anchors to the end. slashes (/) are escaped as \/.



Related Topics



Leave a reply



Submit