Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.htaccess rewriting php pagination

I have reviewed multiple threads on this topic, but all of them are dealing with multiple parameters, and me being quite new with .htaccess I wasn't able to figure out how to modify the answers to fit what I need.

I am rewriting URLs using .htaccess and everything seems to be going well except when I tried rewriting the pagination URLs.

My pagination urls are quite simple: blog.php?page=2 but I cannot rewrite the url to /blog/page/2 for some reason. I get an "Internal Server Error" when I try to route it to that URL. Right now I have it rewritten to /blog-page/2 which is okay, but I would prefer to have it /blog/page/2.

I have a lot going on in my .htaccess file, so I'll only post the rewrites:

RewriteEngine On

# Full path
#RewriteBase /

# Rewrite all php files    
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

# Rewrite the user profile pages
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^user/([a-zA-Z0-9_-]+)$ profile.php?user=$1
RewriteRule ^user/([a-zA-Z0-9_-]+)/$ profile.php?user=$1

# Rewrite the company profile pages
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^company/([a-zA-Z0-9_-]+)$ employer.php?user=$1
RewriteRule ^company/([a-zA-Z0-9_-]+)/$ employer.php?user=$1

# Rewrite the blog post pages
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^post/([a-zA-Z0-9_-]+)$ entry.php?id=$1
RewriteRule ^post/([a-zA-Z0-9_-]+)/$ entry.php?id=$1

# Rewrite the job listing pages
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^job/([a-zA-Z0-9_-]+)$ listing.php?id=$1
RewriteRule ^job/([a-zA-Z0-9_-]+)/$ listing.php?id=$1

# Rewrite blog pagination
RewriteRule ^blog/page/([0-9]+)$ blog.php?page=$1 
RewriteRule ^blog/page/([0-9]+)/$ blog.php?page=$1  

Obviously the problem I am having is with the final RewriteRule's for the blog pagination. Why is it that I cannot make it /blog/page/2 but I can make it /blog-page/2?

I am also trying to do a multiple page .htaccess rewrite for the blog categories and the job categories but I keep getting an internal error. I want 1 rewrite for blog the job categories and the blog categories. Here is what I have for that:

# Rewrite the category queries
RewriteRule ^/category/(.*)/?$ /$.php?cat=$1 [L,QSA]

Is it possible to make this pagination rewrite work for 2 different pages? Such as blogs.php and jobs.php?

Also, is it necessary to keep putting the RewriteCond %{REQUEST_FILENAME} !-f before I make a rewrite rule? Or is 1 time sufficient?

like image 564
Ty Bailey Avatar asked Feb 05 '26 02:02

Ty Bailey


1 Answers

Have you checked the apache error log for info on the error message? If not, that's the first place to start whenever you get a 500 internal error message.

Take a look at RewriteCond in the apache manual. It appears that you're expecting the RewriteCond to work for multiple rules, but that's not how it works. A RewriteCond only applies to the next RewriteRule. You can chain multiple RewriteCond lines together for a single RewriteRule but if you want a RewriteCond to apply to RewriteRules, you'd have to put it before each one.

The good news is most of your rules don't need a RewriteCond:

RewriteEngine On

# Rewrite all php files    
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]

# Rewrite the user profile pages
RewriteRule ^user/([a-zA-Z0-9_-]+)/?$ profile.php?user=$1 [L]

RewriteCond 
# Rewrite the company profile pages
RewriteRule ^company/([a-zA-Z0-9_-]+)/?$ employer.php?user=$1 [L]

# Rewrite the blog post pages
RewriteRule ^post/([a-zA-Z0-9_-]+)/?$ entry.php?id=$1 [L]

# Rewrite the job listing pages
RewriteRule ^job/([a-zA-Z0-9_-]+)/?$ listing.php?id=$1 [L]

# Rewrite blog pagination
RewriteRule ^blog/page/([0-9]+)/?$ blog.php?page=$1 [L]

I made a few changes to your rules:

  1. Removed most of the RewriteCond rules. If you plan to have some static files in any of the directories, you'll need to add the RewriteCond %{REQUEST_FILENAME} !-f rule for them to be served.
  2. Consolidated the rules to handle the trailing / by adding a ? to make the slash optional.
  3. Added [L] to your rules to indicate that no other rules need to be run after a match is found and executed. Take a look at RewriteRule in the apache manual for more details on the available flags.
like image 159
bradym Avatar answered Feb 09 '26 09:02

bradym



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!