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]
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
How to get Stack Overflow SEO friendly URL structure in Nuxt.js?
The following works for me, but I am not sure if it's the exact same as how the Stack Overflow URL structure works.
I am using async asyncData
to get content from the database, and as you can access
context and redirect
, req
and res
as parameters you can perform a 301
redirect.
First I am using unknown dynamic nested routes in my folder like this /pages/folder/_.vue
. Which will catch all routes including domain.com/folder/{id}
and domain.com/folder/{id}/{title}
.
To get the ID of in the requested url you can split the pathMatch and get the first element, like this params.pathMatch.split('/')[0]
.
Then I use the id to get the content from the database, which is Strapi in my case. Like this await $strapi.findOne('contentType', id)
.
After that we can create the actual URL we want like this /folder/${data.id}/${data.slug}
. Note: the data.slug
culd be replaced with the data.title
which could be converted to a URL friendly string.
Finally we can match the user requested URL with the actual URL of the content if(route.fullPath !== actualURL)
, and if the requested url is not the same we can performe a redirect with redirect(301, actualURL)
.
My entire code:
async asyncData({ redirect, req, route, app, $strapi, error, params }) {
try {
const recipeID = params.pathMatch.split('/')[0];
const matchingRecipe = await $strapi.findOne('recipes', recipeID);
const correctPath = `${app.localePath('recipes')}/${matchingRecipe.id}/${matchingRecipe.slug}`;
if(route.fullPath !== correctPath) {
console.log(`Redirect: ${route.fullPath} => ${correctPath}`);
redirect(301, correctPath);
}
return {
recipe: matchingRecipe
}
} catch(e) {
console.log(e)
error({ statusCode: e.statusCode, message: e.original });
}
},
Seo friendly URL - .htaccess
This is a THUMB rule. If current directory has a .htaccess with RewriteEngine ON
then it overrides parent .htaccess directives. If you want parent .htaccess rules to be applied before then use following option:
RewriteOptions InheritBefore
Before RewriteEngine On
line.
So your htaccess file inside /news/ folder will become like:
RewriteOptions InheritBefore
RewriteEngine ON
RewriteRule ^([^/\.]+)$ entry.php?slug=$1 [NC,L]
Additional suggestion: In case you are trying to rewrite non existing files and directories if this is the case then have your htaccess in your news folder like as follows.
RewriteOptions InheritBefore
RewriteEngine ON
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/\.]+)$ entry.php?slug=$1 [NC,L]
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]
Related Topics
PHP How to Remove Last Part of a Path
Is MySQLi_Multi_Query Asynchronous
Get Number of Rows Matched by Update Query with PHP MySQLi
Multi Checkbox Fields in Woocommerce Backend
How to Get Around or Make PHP JSON_Decode Not Alter My Very Large Integer Values
What's the Best Way to Create a Single-File Upload Form Using PHP
PHP - How to Send an Array to Another Page
Which Is Faster? Constants, Variables or Variable Arrays
How to Use Perl Libraries from PHP
Commenting Interpreted Code and Performance
How to Extract HTML Comments and All HTML Contained by Node
Empty String Comparison to Zero Gives Different Result in PHP 8 Than in Previous Versions