Sheldon C
Sheldon C

Reputation: 77

.htaccess not working as I would hope, need help on how to redirect pagenation URL ?page=1 to /page/1/ for example without messing up page content

Answer: Added <base href="<?= dirname($_SERVER['PHP_SELF']); ?>/" /> to head section of /blog and /blog/category, to define the base path.

I have a website where I created a custom blog, the way my pagenation works is by adding a URL parameter to define the current page you are on. I was able to direct the url to pull the correct page, so now i can go to /page/1 instead of ?page=1, however, all the content and links on the page are no longer pointing correctly because the browser thinks the page now lives in a different director.

For example:

My Current Code:

RewriteEngine On

#Internal rewrite for /blog/?page=1 to /blog/page/1
RewriteRule ^blog/\?page=([0-9]+)$ /blog/page/$1 [L]

# Internal rewrite for /blog/page/1 to the original format
RewriteRule ^blog/page/([0-9]+)$ /blog/?page=$1 [L]

# Internal rewrite for /blog/category/test-cat-1/?page=1 to /blog/category/test-cat-1/page/1
RewriteRule ^blog/category/([^/]+)/\?page=([0-9]+)$ /blog/category/$1/page/$2 [L]

# Internal rewrite for /blog/category/test-cat-1/page/1 to the original format
RewriteRule ^blog/category/([^/]+)/page/([0-9]+)$ /blog/category/$1/?page=$2 [L]

I have done much digging only, however every solution I come across, such as adding a RewriteCond directives to check for the presence of the page query string parameter before rewriting the URL. I was told that this should ensures that the query string is preserved, and should help with the content not displaying correctly, however it does not, usually results in a 503 error.

Does anyone have any suggestions, or know of any workaround?

I created this Blog using PHP, this is apache, I would really like a route of just utilizing .htaccess file, but maybe it is not possible?

Any help is much appreciated!

Note:

Upvotes: 1

Views: 22

Answers (0)

Related Questions