Diwakar Kumar
Diwakar Kumar

Reputation: 27

Pagination URL Structure In Htaccess

I have a URL Structure as mysite.com/category.php?c=abc&page=4 I need to have a URL Structure as mysite.com/category/abc/page/4

My Htaccess File code for this rewrite looks like this.

RewriteEngine On
<IfModule mod_rewrite.c>
    https redirect Rule Here
</IfModule>
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php
RewriteRule ^([-\w]+)$ blog-post.php?slug=$1 [NC,L]
RewriteRule ^amp/([-\w]+)$ amp/amp.php?slug=$1 [NC,L]
RewriteRule ^mirror/([-\w]+)$ mirror/post.php?slug=$1 [NC,L]
RewriteRule ^category/([a-zA-Z-]+) category.php?c=$1 [NC,L]
RewriteRule ^category/([a-zA-Z-/]+)/page/([0-9]+) category.php?c=$1&page=$2 [NC,L]
RewriteRule ^mirror/category/([a-zA-Z-]+) mirror/category.php?c=$1 [NC,L]
RewriteRule ^feed/([a-zA-Z-]+)$ feed/feed.php?f=$1 [NC,L]
RewriteRule ^author/([a-zA-Z0-9-]+) author.php?name=$1 [NC,L]
RewriteRule ^tag/([a-zA-Z0-9-]+) tag.php?t=$1 [NC,L]
RewriteRule ^mirror/tag/([a-zA-Z0-9-]+) mirror/tag.php?t=$1 [NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [R=301,L]
RewriteRule ^sitemap\.xml/?$ sitemap.php
RewriteRule ^news-sitemap\.xml/?$ news-sitemap.php
RewriteRule ^image-sitemap\.xml/?$ image-sitemap.php
RewriteRule ^author-sitemap\.xml/?$ author-sitemap.php

ErrorDocument 404 /404.php
ErrorDocument 500 Error
Options -Indexes

The problem I am facing is when I try to get the page number using the $_get[] variable, I am not able to fetch it. Rest I am able to get the ABC through $_get[]

Upvotes: 2

Views: 88

Answers (2)

anubhava
anubhava

Reputation: 785008

You have big set of rules. Let me make an attempt to get you going:

ErrorDocument 404 /404.php
Options -Indexes -MultiViews
RewriteEngine On
<IfModule mod_rewrite.c>
    https redirect Rule Here
</IfModule>

## Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^(.+)/+$
RewriteRule ^ %1 [R=301,NE,L]

## ignore all URIs for actual files and directories
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]

RewriteRule ^amp/([-\w]+)$ amp/amp.php?slug=$1 [NC,L,QSA]
RewriteRule ^mirror/([-\w]+)$ mirror/post.php?slug=$1 [NC,L,QSA]
RewriteRule ^category/([\w-]+)$ category.php?c=$1 [NC,L,QSA]
RewriteRule ^category/([\w-]+)/page/(\d+)$ category.php?c=$1&page=$2 [NC,L,QSA]
RewriteRule ^mirror/category/([\w-]+)$ mirror/category.php?c=$1 [NC,L,QSA]
RewriteRule ^feed/([\w-]+)$ feed/feed.php?f=$1 [NC,L,QSA]
RewriteRule ^author/([\w-]+)$ author.php?name=$1 [NC,L,QSA]
RewriteRule ^tag/([\w-]+)$ tag.php?t=$1 [NC,L,QSA]
RewriteRule ^mirror/tag/([\w-]+)$ mirror/tag.php?t=$1 [NC,L,QSA]

RewriteRule ^sitemap\.xml$ sitemap.php [NC,L]
RewriteRule ^((?:news|image|author)-sitemap)\.xml$ $1.php [NC,L]

RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

RewriteRule ^([-\w]+)$ blog-post.php?slug=$1 [QSA,L]

Make sure to test it after clearing your browser cache completely or test it from command line curl.

Upvotes: 3

RavinderSingh13
RavinderSingh13

Reputation: 133458

With your shown samples, please try following. Please make sure to clear your browser cache before testing your URLs.

There are 2 important points you need to take care. 1st- You didn't use $ anchor which makes sure your 1st rule matching only URI like http://locahost:80/category/testtest so what's happening your rules are matching both the URIs. 2nd- Is you need to add conditions to make sure these rules are applied to only non existing stuff(in ideal scenario).

RewriteEngine On
Options -Indexes
<IfModule mod_rewrite.c>
    ##https redirect Rule Here
</IfModule>
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^(.*)$ $1.php [L]

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^([-\w]+)$ blog-post.php?slug=$1 [NC,L]
RewriteRule ^amp/([-\w]+)$ amp/amp.php?slug=$1 [NC,L]
RewriteRule ^mirror/([-\w]+)$ mirror/post.php?slug=$1 [NC,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^category/([a-zA-Z-]+)/?$ category.php?c=$1 [NC,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^category/([a-zA-Z-/]+)/page/([0-9]+)/?$ category.php?c=$1&page=$2 [NC,L]

RewriteRule ^mirror/category/([a-zA-Z-]+) mirror/category.php?c=$1 [NC,L]
RewriteRule ^feed/([a-zA-Z-]+)$ feed/feed.php?f=$1 [NC,L]
RewriteRule ^author/([a-zA-Z0-9-]+)$ author.php?name=$1 [NC,L]
RewriteRule ^tag/([a-zA-Z0-9-]+)$ tag.php?t=$1 [NC,L]
RewriteRule ^mirror/tag/([a-zA-Z0-9-]+)$ mirror/tag.php?t=$1 [NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [R=301,L]
RewriteRule ^sitemap\.xml/?$ sitemap.php [NC,L]
RewriteRule ^news-sitemap\.xml/?$ news-sitemap.php [NC,L]
RewriteRule ^image-sitemap\.xml/?$ image-sitemap.php [NC,L]
RewriteRule ^author-sitemap\.xml/?$ author-sitemap.php [NC,L]

ErrorDocument 404 /404.php
ErrorDocument 500 Error

Upvotes: 1

Related Questions