.htaccess rewriting php pagination - .htaccess

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?

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:
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.
Consolidated the rules to handle the trailing / by adding a ? to make the slash optional.
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.

Related

htaccess rewrite key/value pair params in url

I know this is very common question but I can't seem find any solution regarding in my problem. The folder structure looks like this:
- rootProject
- subfolder
* .htaccess
* index.php
Normal url params: http://website.com/subfolder/?dynamicKey=dynamicValue.
But What I want to achieve is like this: http://website.com/subfolder/dynamicKey/dynamicValue
But I failed to do that, I can only retrieve the dynamicKey.
Here is my current htaccess:
RewriteEngine On
RewriteRule ^([a-z]+)/?$ index.php?$1 [NC,QSA,L]
Thanks to #Ar Rakin. Here is the solution.
RewriteEngine On
RewriteRule ^([a-z]+)/?$ index.php?$1 [NC,QSA,L]
RewriteRule ^([a-z]+)/([a-z]+)/?$ index.php?$1=$2 [NC,QSA,L]
With your shown samples and attempted code, please try following htaccess rules. We need to add conditional checks before rewrite rules else later it will affect your existing pages also.
Make sure to clear your browser cache before testing your URLs.
RewriteEngine On
##Added conditions if non-existing pages requested then only perform rewrite.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z]+)/?$ index.php?$1 [NC,QSA,L]
##Added conditions if non-existing pages requested then only perform rewrite.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z]+)/([a-z]+)/?$ index.php?$1=$2 [NC,QSA,L]
NOTE: your used regex [a-z] will only match small letters in uri, in case it needed to check any alphabets(capital OR small) then change FROM [a-z] TO [a-zA-Z] in above rules also.

rewriting url with .htacces cause files not to be found

In general, I am trying to understand how .htaccess works. I would highly appreciate it if anyone points me in the right direction. I have been trying to make the following url (with optional parameters) pretty.
mysite/v1.0/foldername
mysite/v1.0/foldername/param1/
mysite/v1.0/foldername/param1/param2/etc
I tried the following:
RewriteEngine On
RewriteCond %{REQUEST_URI} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)/?$ foldername.php [QSA,L]
the problem is that when I get it to pass the parameters it can no longer get the resources. It seems to have changed directory.
.htaccess is in foldername
Also, I would like to know what site i can go to to learn about REQUEST_URI, REQUEST_FILENAME, etc. A site that is not too technical as it's the apache site.
You are incorrectly rewriting the rules correct rule according to your need would be like,
RewriteEngine On
# rule for removing extension
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([\w-]+)/?$ $1.php [QSA,L]
# below cond means incoming url is nor a file neither a directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
# actual rules
RewriteRule ^([\w-]+)/([\w-]+)/?$ $1.php?param1=$1 [L]
RewriteRule ^([\w-]+)/([\w-]+)/([\w-]+)/?$ $1.php?param1=$2&param2=$3 [L]
Refrences
Reference: mod_rewrite, URL rewriting and "pretty links" explained
http://httpd.apache.org/docs/current/mod/mod_rewrite.html

CodeIgniter: add custom rewrite rule to redirect from static file to controller

Lets say that I have a static app and a JSON file that feeds it.
The problem is that the JSON file it's not available yet and I can't use it, so, to make the application run in development environment, I want to add a rewrite rule to redirect the JSON request to a CI controller path that will return a JSON as well.
My htaccess file looks like this
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|images|assets|static|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
#Rewrite for development
RewriteRule ^static/app/json/appData.json$ /buscador/getAppData
The first condition is the CI main condition to redirect all request to index.php
Then I added the RewriteRule for the JSON file that's going to be located in static/app/json/appData.json and it needs to be redirected to the CI controller buscador/getAppData
The thing is that this Rewrite Rule isn't working and I don't know why, can somebody tell me what is wrong o how can I add a custom rewrite rule in CI?
Thank you!
I was pondering on a similar problem for quite a few hours. I needed to add a custom RewriteRile so that a shorter link should be generated (and working) in order to be sent as a footer in an SMS to users. I finally managed to do it with a 301 redirect before the CI redirects. Here's my .htaccess:
Options -Indexes +FollowSymLinks -MultiViews
RewriteEngine on
RewriteBase /
#Custom redirect
RewriteRule ^nv([0-9]+?)?$ /news/locate/item/$1 [NC,L,R=301]
#Remove trailing slash against SEO duplicates
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*)/$ /$1 [L,R=301]
#Generic controller redirects
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
Now http://domain.tld/nv1234 redirects to http://domain.tld/news/locate/item/1234 as intended, although not as transparently as I would like it to, but I couldn't find a better way.
Please note the R=301 in the Custom redirect line - the RewriteRule will not work if the client is not redirected to the newsController.
Posting my solution here with the hope that other developers will not get lost as I (and the OP) did.

htaccess rewrite for subdomain only

I'm trying to rewrite some parameters to beautiful links, but for a subdomain / a folder only. Unfortunately I can't get it to work, maybe also because there are some other rewrites in line before...
Heres my code:
<IfModule mod_rewrite.c>
# NON-WWW TO WWW
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
# WORDPRESS-BLOG
Options +FollowSymlinks
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# REDIRECT FOR SUBDOMAIN
RewriteCond %{HTTP_HOST} ^subdomain.example.com
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/.]+)(?:/)?$ index.php?cshort=$1 [L]
RewriteRule ^([^/.]+)/([^/.]+)(?:/)?$ /index.php?cshort=$1&cid=$2 [L]
RewriteRule ^([^/.]+)/([^/.]+)/([^/.]+)(?:/.*)?$ /index.php?cshort=$1&cid=$2&step=$3 [L]
</IfModule>
Basically only the last part is the one I want to rewrite to change URLs from something like
http://subdomain.example.com/index.php?cshort=abc&cid=123&step=1 to http://subdomain.example.com/abc/123/1
The other rewriting rules for www.example.com shouldn't get affected. Unfortunately my current codes only does the first two rules for the blog and the www, but nothing happens on the subdomain. What's wrong in my code?
When you say that you want to rewrite from http://subdomain.example.com/index.php?cshort=abc&cid=123&step=1 to http://subdomain.example.com/abc/123/1 you mean that you want the user to enter the pretty URL and to have it serve the full URL in the background, not that you want to redirect from the ugly to the pretty URL, right?
In your RewriteRules, what are you trying to accomplish with "(?:/)?"? As written, that doesn't make any sense to me. If you're just trying to match whether or not the directory path ends with a slash, you can do that as follows:
RewriteRule ^([^/.]+)/?$ index.php?cshort=$1 [L]
EDIT: Additional suggestions:
Move the "Redirect for subdomain" section above the "Wordpress Blog" section. Since the Wordpress rule applies to "everything that's not a real file or directory, regardless of domain" that should go last.
RewriteConds only apply to a single RewriteRule that follows them. For each of the three rules you have listed under "Redirect for subdomain", after updating them per the above suggestion, you need to repeat the two RewriteCond lines in front of the RewriteRule.

Limit RewriteRule in htaccess file

i got a site php based with some rules in the .htaccess file to get rid of file extentions in the url adress bar. Basically it takes http://netbureau.com.br/en/about.php/ and turns it into http://netbureau.com.br/en/about.
Here are the lines in the htaccess file:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
The problem comes when i try to access the rss feed of the blog at http://netbureau.com.br/blog/?feed=rss2 and when i try to set custom permalinks for the blog at http://netbureau.com.br/blog. It gets messed up by the htaccess file.
So is there any way to disallow the RewriteRule for the /blog folder so that i can get back my rss link and set custom permalinks in the blog?
I know it's at the same time Wordpress related but it feels more connected to the htaccess file than Wp.
Thanks in advance.
EDIT #1:
I've set up the wordpress permalinks to the default structure which goes like this: http://netbureau.com.br/blog/?p=123 This made my rss link back for good.
The remaining problem is that Wordpress gives me its own rewriterule which is:
RewriteEngine On
RewriteBase /blog/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]
Is there a way to to still use the first rule to apply to the whole site except the /blo/ folder and apply the WP rule only to the /blog/ folder?
I've tried different combinations but without luck so far. I could only have the site without the custom links for the blog or the custom blog links and a 404 on the pages of the site.
Try:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/blog/?
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
The !^/blog/? expression excludes any URI that starts with /blog

Resources