Emilian
Emilian

Reputation: 80

.htaccess how to load rewrited page instead redirect

I have a pure HTML/CSS/PHP blog. Structure is simple. Main page is domain.com/blog (latest posts) and domain.com/blog?page=2 (for posts in page 2 etc). That page is controlled by blog.php file. If user want to read post for example "How-to-learn-polish" he goes to domain.com/blog_post?name=How-to-learn-polish and this is also work.

My problem is, how to write rule in htaccess to load content of page domain.com/blog_post?name=How-to-learn-polish in url domain.com/blog/How-to-learn-polish

I searched on internet solution for my problem and i create that rule

RewriteEngine On
RewriteRule ^blog/([^/]*)$ blog_post.php?name=$1 [L,NC]

This rule works ok in 80% because it redirect from domain.com/blog/How-to-learn-polish to domain.com/blog_post?name=How-to-learn-polish

I don't want to redirect but load post in this URL domain.com/blog/How-to-learn-polish

What i should to change in my htaccess to do that? Full of my htaccess look like this

Header set Cache-Control "max-age=31536000, public"

RewriteEngine On
RewriteRule ^blog/([^/]*)$ blog_post.php?name=$1 [L,NC]

RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]

RewriteBase /
RewriteRule ^(.+)\.php$ /$1 [R,L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ /$1.php [NC,END]

Upvotes: 1

Views: 37

Answers (1)

anubhava
anubhava

Reputation: 785491

You rules need reordering:

Header set Cache-Control "max-age=31536000, public"

RewriteEngine On

RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301,NE]

RewriteRule ^(.+)\.php$ /$1 [R=301,NC,L]

RewriteRule ^blog/([^/]+)/?$ blog_post.php?name=$1 [END,NC,QSA]

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ /$1.php [END]

Make sure to use a different browser or remove cache data from your browser to test this rule to avoid old cache.

Upvotes: 1

Related Questions