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
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.
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).
Your attempt was close. Just a small fix:
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:
It is a good idea to start out using a
R=302
_temporary_redirection and to only change that to aR=301
permanent redirection once everything works as expected. That prevents nasty caching issues on the client side.