skip to Main Content

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]

2

Answers


  1. 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).

    Login or Signup to reply.
  2. Your attempt was close. Just a small fix:

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

    This relies on additional rewriting rules to get applied though, since /blog_template most likely is not a resource the http server can somehow "execute" immediately. So you may want to combine above rule with other rules, which you did not reveal to us, which is why I cannot say anything about that.

    If you also want to redirect requests to the "old" URL, then that variant should do:

    RewriteEngine on
    
    RewriteCond %{QUERY_STRING} ^slug_url=([^/]+)$
    RewriteRule ^/?blog_template$ / [QSD,R=301,L]
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^/?([^/]+)/?$ /blog_template?slug_url=$1 [L] 
    

    It is a good idea to start out using a R=302 _temporary_redirection and to only change that to a R=301 permanent redirection once everything works as expected. That prevents nasty caching issues on the client side.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search