+3 votes
Seo positioning from PHP and Apache

in SEO by (487k points)
reopened | 212 views

1 Answer

+4 votes
Best answer

An important part of the SEO techniques are the friendly URLs are characterized by being easy to remember for the user, descriptive about the content of the web and easy to write.

In this tutorial we will learn how to make friendly URLs in PHP, so that our software can return the SEO URL based on the title.

To do this we will create a PHP function that will translate a sentence that can be taken from a database or entered in a form and turn it into a search engine friendly url.

: The function will be :


This function traces the phrase character by character and replaces those that may not be readable by browsers.

: Example of url conversion :

<? $ title = 'registration for the competition';
$ url = getUrlAmigable (utf8_decode ($ title)); ?>

<a href="www.midominio.com/<?= $url?> "/> <? = $ title?> </a>
The url will be
<a href="www.mydomain.com/registration-for-competition"> competition registration </a>

Here you can see an example of how it would be for twitter and different friendly url


When we have the conversion code working we need to create a file that will communicate with the Apache server and allow the web to understand our friendly URLs.

We create a simple text file that will be called .htaccess in the main directory of the web where this is our index.php file.

In the .htacess file : We write the following code :

# Allow rewrite url
RewriteEngine On
# The URL is for .htaccess and is not a directory
RewriteBase /
# The url is fictitious is not an existing file on the server.
# The url is fictitious is not an existing directory on the server.
# But it is not an existing file and it is not a directory. I access the index.php and assign the friendly url as a variable
RewriteRule ^ ([a-zA-Z0-9 _-] +) $ index.php? Section = $ 1

Finally in the file index.php I read that variable that secretly sends me the .htacess


An important issue is the security with this method since someone could try to inject code through index.php? Section = 'pagina-malicionsa', to avoid this we create a registry of valid files that can be accessed ie physical files on the server.

To do this we define a section after $ section = $ _ GETseccion '], a matrix that will contain the names of the files on the server.

<? $ list_sections = array ("users", "login", "help", "products", "index"); ?>

So if someone tries to access an invalid URL it will be redirected to an error page or if you have not selected any url by default it will go to a page called home.php.

We can also use it to go to different sections of the web for example

URL <a href="vendo-casa-en-barcelona-im100"> I'm selling a house in barcelona </a>

In the .htaccess I can put the following rule

#If the address contains "im" the RewriteRule that would be below is executed
RewriteCond% {REQUEST_URI} im-
Rewriterule ^ (. +). Php ./index.php?seccion=inmuebles&cod=$1

in this way the title and the code of the house would be passed as variable and then processed by database

<a href="listado-CL50"> I sell a house in Barcelona </a>

#If the address contains "CL" the RewriteRule that would be below
RewriteCond% {REQUEST_URI} CL
Rewriterule ^ (. +) ./Index.php?seccion=listarclients&limite=$1

Here it could be to list client for the number 50 or the client 50 according to the task that we want to perform later in our database. We will read the values ​​of the variables in the index.php with $ GET, that is, for the last case it would be $ GET ['limit']

by (3m points)

Related questions

+4 votes
1 answer
asked Jun 24, 2019 in SEO by backtothefuture (487k points) | 153 views
+5 votes
1 answer
+5 votes
1 answer
+5 votes
1 answer
asked Jun 24, 2019 in SEO by backtothefuture (487k points) | 126 views
+3 votes
1 answer
asked Jun 24, 2019 in SEO by backtothefuture (487k points) | 134 views
Sponsored articles cost $40 per post. You can contact us via Feedback
9,349 questions
9,465 answers
2 users