简体   繁体   中英

seo friendly url for blogs

am building a blog system and i have a table a in my database called url which save the post title with the date i also have a page called index.php with display all the data from database, which when click take you to post.php .the problem is that the url is not SEO friendly site/post?url=seo-friendly-url.html/2017-09-29 ,i have a .htaccess file which works well

`Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [QSA,L]
RewriteRule ^post/([a-zA-Z0-9-/]+)$ post.php?url=$1  
RewriteRule ^post/([a-zA-Z-0-9-]+)/ post.php?url=$1  
`

but refuse to rewrite my blog post,my index hyper link look like this <a href='post?url=".$row['url']."'> , have tried to rewrite it to <a href='post/".$row['url']."'> but is not working,have made research but still no progress,please any help would be much appreciated,as have been on the issue for some days now.

Add AliasMatch in VirtualHost file of your website (This will be located in your apache folder)

These are the lines you need to add in Virtual host file

AliasMatch ^post/([a-zA-Z0-9-/]+)$ post.php?url=$1  
AliasMatch ^post/([a-zA-Z-0-9-]+)/ post.php?url=$1

In linux based server. The virtual host will be located in the folder

/etc/apache2/sites-enabled/ 

Here in this folder find your website virtual host file and add these two lines

Virtual host will have code similar to this

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined


    #Add these two line only donot disturb any other lines 

    AliasMatch ^post/([a-zA-Z0-9-/]+)$ post.php?url=$1  
    AliasMatch ^post/([a-zA-Z-0-9-]+)/ post.php?url=$1


</VirtualHost>

In your .htaccess file Remove these two lines

RewriteRule ^post/([a-zA-Z0-9-/]+)$ post.php?url=$1  
RewriteRule ^post/([a-zA-Z-0-9-]+)/ post.php?url=$1

Bonus

Now if you want to redirect the users who are visiting to old url (/post?url="old_url.html") to new seo friendly url (/post/old_url.html) add this to your .htaccess file

RewriteRule ^post.php?url=([a-zA-Z0-9-/]+)$ post/$1
RewriteRule ^post.php?url=([a-zA-Z-0-9-]+)/ post/$1

If you have all the lines as comments(# infront of all lines) in virtual host file add this to your virtual host at the bottom

 <VirtualHost *:80>
        DocumentRoot "C:/xampp/htdocs"
        AliasMatch ^post/([a-zA-Z0-9-/]+)$ post.php?url=$1  
        AliasMatch ^post/([a-zA-Z-0-9-]+)/ post.php?url=$1


    </VirtualHost>

**

Remember to restart your Apache Server after you do the changes

**

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM