Search code examples
apache.htaccesshttp-redirectmod-rewritefriendly-url

how to change url to a clean url that works


I've been looking for an answer for my problem, however the things I tried didnt work out. What I've been trying to do is to create a beatiful url for this link:

mywebsite.com/blog_template?slug_url=blog-post-name

to

mywebsite.com/blog-post-name

To achieve this I tried the following code:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^blog_template/([^/]+)$ /blog_template?slug_url=$1 [L] 

But my code didnt work... Any advice?

Here’s the full htaccess my website:

RewriteEngine On
RewriteBase /
ErrorDocument 404 http://91.218.67.117/404/ 
ErrorDocument 500 http://91.218.67.117/500/  
#redirect 404  
RewriteCond %{REQUEST_URI} ^/500/$ 
RewriteRule ^(.*)$ /pages/errors/500.php [L]  
#remove php 
RewriteRule ^(.+)\.php$ /$1 [R,L] 
RewriteCond %{REQUEST_FILENAME}.php -f 
RewriteRule ^(.*?)/?$ /$1.php [NC,END]

Solution

  • Considering you want to rewrite this request in backend to index.php(OR change it to appropriate file's name in case its some other php file). With your shown samples and attempts please try following .htaccess Rules.

    Please make sure to clear your browser cache before testing your URLs.

    RewriteEngine ON
    RewriteBase /
    RewriteCond %{THE_REQUEST} \s/blog_template\?slug_url=(\S+)\s [NC]
    RewriteRule ^ /%1? [R=301,L]
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)/?$ index.php?slug_url=$1 [QSA,L]
    
    ErrorDocument 500 /pages/errors/500.php
    

    NOTE: Please keep your .htaccess file and index.php(OR any php file which is taking rewrite request in backend for that matter) in same path(your root etc).