How to rewrite two GET values on htaccess and mod-rewrite - .htaccess

I just want to make the GET information that I sent:
http://mywebsite.com/folder/index.php?category=category-alias-here
http://mywebsite.com/folder/index.php?category=category-alias-here&page=5
To be rewrited on the URL bar as:
http://mywebsite.com/folder/category-alias-here/
http://mywebsite.com/folder/category-alias-here/5/
And I cant. Its very simple, I saw several similar questions. Tested several examples on stackoverflow and they all give me 404 for both category and both pages.
Here is a code that I tested:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ index.php?category=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)$ index.php?category=$1&page=$2 [L]
I understand that this code be wrong for my structure specifically, I tried several changes placing and removing ([^/]+) or changed the fist [L] to [N]. And several other things. But no luck with that.
Please notice, the whole website is inside a real directory (/folder/) and the .htaccess is also there (its not on root), once I launch the website everything will be on public_html, but I do not think this is the reason of this issue.
And yes, mod_rewrite is on. There is actually a .htaccess that I use, it works anywhere I place and it gets the category correctly but it is a bit messy.

Found a solution:
Options -Indexes
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([a-z-]+)\/([0-9-]+)\/$ index.php?category=$1&page=$2 [NC,L]
RewriteRule ^([a-z0-9-]+)\/$ index.php?category=$1&page=$2 [NC,L]

Related

Redirecting all requests from a subdirectory to a specific file

I wanted to redirect all requests from a specific subdirectory (/portfolio/*) to a specific file in the root of my website (portfolio.php). This way I want to be able to create project-specific pages within the same file as where the overview is being created.
I tried the following, but it didn't work since it stopped images from loading (images were located in the /images/portfolio/* directory), and it also disables my custom ErrorDocument...
RewriteRule /portfolio/(.*) /$1 [PT]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /portfolio.php [L]
Anyone has an idea on how to fix this?
Try getting rid of the first rule and adding the pattern to your last rule:
RewriteRule ^portfolio.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^portfolio/(.+)$ /portfolio.php [L]
And for good measure, make sure multiviews is turned off:
Options -Multiviews

Moving codeigniter project to a new hosting provider - .htaccess and index.php

I just moved my functional codeigniter project to a new web hosting provider and am now having challenges removing the index.php from the URL using a standard .htaccess mod-rewrite. Here is the .htaccess that was working fine:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
I found this discussion but I do not have root access to the apache server to make the suggested configuration change.
Using the provided .htaccess file above,
Works fine: http://www.mysite.com/index.php/plans
Doesn't work: http://www.mysite.com/plans
Any suggestions are appreciated.
If you really have to accept both forms of URLs likewise then you need two RewriteRules. Once accepting variants with then 'index.php' part and one without.
Something like this:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^index\.php/(.*)$ index.php/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
Your question's not very clear, but a common problem with mod_rewrite rules like this is that some servers need the request to be passed as a query. I.e. you'd need to change your last line to:
RewriteRule ^(.*)$ index.php?/$1 [L]

Trouble with htaccess mod_rewrite and directories

I've read dozens of posts about similar problems, but I can't get this one figured out.
I have SEO friendly URL's on my site that look like this...
http://www.website.com/tequila/bottle-name.html
http://www.website.com/whiskey/bottle-name.html
http://www.website.com/vodka/bottle-name.html
...where bottle-name is the actual name of the bottle.
I am doing a mod_rewrite to a PHP handler to actually deliver the pages. Each handler is located in each directory (tequila, vodka, whiskey).
The first one (tequila) is working, but the other two are not and I can't figure out why.
Here is the mod_rewrite code I have right now...
#Rewrite seo urls
RewriteCond %{REQUEST_URI} !^/tequila/
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\.html$ index.php?liquor=$1&liquor-type=tequila [L]
RewriteCond %{REQUEST_URI} !^/whiskey/
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\.html$ index.php?liquor=$1&liquor-type=whiskey [L]
RewriteCond %{REQUEST_URI} !^/vodka/
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\.html$ index.php?liquor=$1&liquor-type=vodka [L]
What appears to be happening is the whiskey and vodka pages are getting caught and processed by the tequila mod_rewrite (guessing because it is first in the list here).
So, http://www.website.com/vodka/vodka-bottle.html is actually getting rewritten as http://www.website.com/tequila/index.php?liquor=vodka-bottle.html&liquor-type=tequila.
It seems like I haven't setup the RewriteCond %{REQUEST_URI} !^/tequila/ correctly?
Any help or pointers anyone can provide are greatly appreciated!
Why are you negating the ^/tequila/? The ! will cause a match on all other liquor types, rather than the one you list.

RewriteRule on litespeed question

My rule works good when I access http://domain.com/example . The Query looks good etc.
When I visis http://domain.com the index.php does not load, seem that a blank redirect is done, http://domain.com/city.php?q=
How can I get the index.php to load instead of a the Rewrite kicking in...
Options +FollowSymLinks
RewriteCond %{REQUEST_URI} !\.(css|jpg|gif|png|jar|js|html|htm|php)$
RewriteEngine On
RewriteRule ^([^/]*)$ /city.php?q=$1 [L]
Thanks
Ok, I got it.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

htaccess rewriterule being ignored

I have an .htaccess file that is not working properly. And it used to.
Here is the file:
RewriteEngine On
# Category
#Edit
RewriteRule ^edit/([^/]+)/?$ /edit_solutions/index.php?category=$1 [L]
RewriteRule ^edit/?$ /edit_solutions/choose.php [L]
#Report
RewriteRule ^reports/([^/]+)/?$ /report/report.php?project=$1 [L]
RewriteRule ^reports/?$ /report/index.php
#View
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ /view/index.php?category=$1
RewriteRule ^$ /view/choose.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
For some reason, links like mydomain.com/edit/butler/ are going to the view condition, not the edit condition. So the view is picking up the category as edit/butler/. Any ideas why the first condition isn't picking up that url correctly?
Edit: I just checked the $_GET contents, and the category is being set to error.html.
I found out the problem wasn't in fact the htaccess file. There was a file called edit.old.php that was somehow being called. I renamed it to old.edit.old.php (good name, I know), and the site works fine now. Leaving this up for reference

Resources