How to Display Seo Friendly Urls Using Mod_Rewrite

How can I display SEO friendly URLs using mod_rewrite?

mod_rewrite would rather be used to do the opposite: rewrite requests of /ModuleType/Category/ProductName internally to /index.php?m=ModuleType&categoryID=id&productID=id. Using the new URLs in the documents is the job of your application.


Edit    Here’s an example of how a function might look like that turns your parameterized URLs into the new ones:

function url($url, $rules) {
$url = parse_url($url);
parse_str($url['query'], $url['query']);
$argNames = array_keys($url['query']);
foreach ($rules as $rule) {
if ($rule[0] == $url['path'] && array_keys($rule[1]) == $argNames) {
$newUrl = $rule[2];
foreach ($rule[1] as $name => $pattern) {
if (!preg_match('/'.addcslashes($pattern, '/').'/', $url['query'][$name], $match)) {
continue 2;
}
$newUrl = str_replace('<'.$name.'>', $match[0], $newUrl);
}
return $newUrl;
}
}
return $url;
}

$rules = array(
array(
'/index.php',
array('m'=>'.*', 'categoryID'=>'.*', 'productID'=>'.*'),
'/<m>/<categoryID>/<productID>'
)
);
echo '<a href="' . url('/index.php?m=ModuleType&categoryID=categoryID&productID=productID', $rules) . '">/ModuleType/Category/ProductName</a>';

using apache's mod_rewrite to parse SEO friendly URL's

This should work for both:

RewriteEngine on
RewriteRule ^([^/]+)/([^/]+).*$ $1.php?id=$2 [L]

Explanation:

^           - beginning of the string
([^/]) - first group that doesn't contain /
will match both 'profile' and 'store'
will also be referenced by $1 later
/ - first slash separator
([^/]) - second group, id in your case
will be referenced by $2
.* - any ending of the request uri
$ - end of request string

You can also make it more precise so only the two request are rewritten and only digits are accepted as id:

RewriteRule ^((profile|store))/(\d+).*$ $1.php?id=$2 [L]

Creating SEO friendly URLs using apache mod_rewrite

You can do:

<ifModule mod_rewrite.c>

# Turn on the engine:
RewriteEngine on

# Redirect certain paths to index.php:
RewriteRule ^(search|contact)/?$ index.php?p=$1 [L,QSA,NC]

RewriteRule ^([\w-]+)/([\w-]+)/?$ index.php?p=$1&user=$2 [L,QSA,NC]

</ifModule>

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.

How to use .htaccess mod_rewrite to make an seo friendly url

RewriteEngine On
RewriteCond %{QUERY_STRING} ^title\=(.+)$ [NC]
RewriteRule ^/?category/?$ /category/%1 [NC,R,L,QSD]
  • We first check to see if there is a query string present using RewriteCond. If it matches ^title\=(.+)$, meaning, if there is a query string with key title, then capture the value of it.

  • Using RewriteRule, we match if request URI has a category. If yes, then rewrite it as /category/%1 where %1 is the value of the title key matched in rewrite condition for a query string. The %1 is a backreference(scroll down a bit) to the captured group number in the regex(of the query string).

  • Note that if you want to do it for any key-value pair in the query string, then change title to \w+ or [a-zA-Z0-9]+ to be more precise.

mod_rewrite for SEO friendly URL using htaccess

Probably extra / at the end (before $) is the problem.

Change it to like this.

RewriteRule ^(.*)/(.*)$ item.php?id=$2&lang=$1 [QSA,L]

mod_rewrite for seo friendly urls with 5 parameters

You can use this rule in the .htaccess in the root.

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ property/happy_property_urls.php?lang=$1&city=$2&type=$3&status=$4&ref=$5 [NC,L,QSA]
</IfModule>

how to rewrite SEO-friendly url markep in htaccess

RewriteRule ^([^/]*)/([^/]*)$ /search.php?loc=$1&q=$2 [L]


Related Topics



Leave a reply



Submit