Mod Rewrite pagination

I’ll have this URL

http://www.codefundamentals.com/archive.php?page=2

That’s an example. I can have page 1, page 4, etcetc. I’d like the eventual URL to look like this

http://www.codefundamentals.com/archive/page/2

Is this the correct code? I don’t think this removes all file extensions (I’d like to know how to do that as a blanket piece of code that applies to all pages.)

RewriteEngine On
RewriteRule ^page/([^/]*)\.php$ /archive.php?page=$1 [L]

Can’t try it since pagination isn’t put into play. I’m gathering all my chess pieces at the moment.

As said above I’d also like to know how to remove file extensions (.php) please. Is this the correct code?

RewriteEngine On
RewriteRule ^(.*)$ $1.php

That tutorial I linked last night, that you didnt bother clicking, walks you through it pretty well.

2 Likes

RR,

First code block:

  1. Don’t include the start anchor because you have a pseudo directory before page.
  2. I wouldn’t use the leading / in the redirection as that redirects first to the server’s root then to the domain’s DocumentRoot.
  3. The .php in your regex can NEVER be matched by “2”. Delete the .php from the regex.

Assuming your mod_rewrite code is located in the DocumentRoot’s .htaccess, those three changes will enable your code to work.

Second code block:

Nope. That will cause an infinite loop (adding .php after .php until the server gives up.

… BECAUSE, as for removing extensions, this is YOUR job in creating your links. THEN, check that the .php version of the {REQUEST_URI} exists before redirecting. That is included in my tutorial at http://dk.co.nz/seo.

Regards,

DK

Thanks for the help dklynn, I’ll review these comments in a bit and see what implementation I can get going.

I’ve been playing with it but I just haven’t had it click yet.

Did two out of the three of what you suggested @dklynn . I’m not familiar with htaccess so I’m nto sure what you mean by getting rid of my anchor.

This is my updated htaccess file.

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.codefundamentals\.com$ [NC]
RewriteRule .? http://www.codefundamentals.com%{REQUEST_URI} [R=301,L]


RewriteEngine on
RewriteRule ^([a-z]+)$ $1.php [L]

RewriteEngine On
RewriteRule ^page/([^/]*)\$ archives.php?page=$1 [L]

I appear to have it working now. However, I notice as I browse throughout my site, that the extensions aren’t being removed. Why is this?

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.codefundamentals.com$ [NC]
RewriteRule .? http://www.codefundamentals.com%{REQUEST_URI} [R=301,L]

RewriteEngine on
RewriteRule ^([a-z]+)$ $1.php [L]

RewriteRule ^archives/([^/]+)/([^/]+)/?$ /archives.php?page=$2 [L]

RR,

The RewriteEngine on is to take mod_rewrite out of Comment Mode. It only needs to be done ONCE (no need to repeat it unless you use RewriteEngine off in an intervening directive).

The first RewriteRule is fine (read excellent).

The second RewriteRule is LOOPY (as mentioned above). TEST whether $1.php exists before making the redirection!

The third RewriteRule allows page/{null} and the end anchor (the $) is NEVER escaped.

Please read the longstanding tutorial at http://dk.co.nz/seo.

On to your second post (#6).

Regards,

DK

Taken from your website :wink: .

I don’t think it’s possible for me to be any more noob at Apache. I have no clue how to do this.

RR,

[quote=“RyanReese, post:6, topic:110205, full:true”]
I appear to have it working now. However, I notice as I browse throughout my site, that the extensions aren’t being removed. Why is this?[/quote]

Oh, really?

This is because, as stated above, YOU must create the extensionless links. mod_rewrite’s job (once you get to that level), is to redirect the extensionless link to a file request that Apache can serve. That’s the short version of what’s at http://dk.co.nz/seo.

[quote=“RyanReese, post:6, topic:110205,”]RewriteEngine onRewriteCond %{HTTP_HOST} !^www.codefundamentals.com$ [NC]
RewriteRule .? http://www.codefundamentals.com%{REQUEST_URI} [R=301,L][/quote]

Still fine - assuming that you’ve ensured that you’re not in Comment Mode.

# Missing RewriteCond is located at http://dk.co.nz/seo
# RewriteEngine on SERIOUSLY? Don't duplicate this (wasted machine cycles).
RewriteRule ^([a-z]+)$ $1.php [L][/code]

You missed the reason for RewriteEngine on in the tutorial (or did I miss that?).

If the second atom is supposed to be a digit, you’ve use someone else’s overkill - and it’s nearly as bad at :fire: (.*) :fire: Moreover, your optional trailing slash will DESTROY your script’s relative links (for one or the other case). Why not

RewriteRule ^archives/.+/([\d]+)$ archives.php?page=$1 [R=301,L]

Test that with the R=301 to confirm that the redriection works then remove it for online use … but make sure that your relative links are relative to the archives/whatever/ directory level.

Regards,

DK

Most of what you said went over my head. I appreciate you helping me.

I did not read your page, I simply went to the code snippets and tried modifying it for personal use.

I might be in over my head here. I don’t understand anything about Apache.

For what it’s worth, this line you gave me is causing 500 server errors.

RewriteRule ^archives/.+/(\d]+$ archives.php?page=$1 [R=301,L]

RR,

Argh! Corrected above.

RewriteRule ^archives/.+/([\d]+)$ archives.php?page=$1 [R=301,L]

Regards,

DK

That redirects me to this.

http://www.codefundamentals.com/home1/codefund/public_html/archives.php?page=2

RR,

It looks like it’s working correctly but Apache is not. I’ve seen this and heard of this problem before and the only solution I’m aware of is a RESTART of the Apache server.

Regards,

DK

I talked to my host and they say that I’d need a VPS to restart it. I’m on shared hosting.

Is there nothing I can do?

Out of curiosity, what happens if you change it to

RewriteRule ^archives/.+/([\d]+)$ ./archives.php?page=$1 [R=301,L]

If I go to

www.codefundamentals.com/archives?page=2

It does nothing.

If I go to www.codefundamentals.com/archives/page/2 manually, I get this URL

http://www.codefundamentals.com/home1/codefund/public_html/archives.php?page=2

I’m willing to give Cpanel login to modify the htaccess so you can more easily play with it. Let me know.

This is weird…

http://www.codefundamentals.com/archives/page/1 – works
http://www.codefundamentals.com/archives/page/1/ – works
http://www.codefundamentals.com/archives/page/2 – weird processing
http://www.codefundamentals.com/archives/page/2/ – works
http://www.codefundamentals.com/archives/page/3 – works
http://www.codefundamentals.com/archives/page/3/ – works

Shouldn’t this redirect to the pagination URL?
http://www.codefundamentals.com/archives.php?page=2

It looks like it’s working backwards lol. The page/2/ is redirecting to ?page=2