htaccess rewrite rule for multiple pages - .htaccess

this seems so simple, but it's not working for me.
I am trying to have both show.php?id=$1 and producer.php?id=$1 rewrite as friendly URLs.
The first one works perfectly, the second does not. If I remove the first, the second works fine.
What am I doing wrong here?
My code:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)\.html$ /show.php?id=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)\.html$ /producer.php?id=$1 [L]
</IfModule>
Thanks in advance!

Your problem is quite simple.
You cannot have more than one rewrite rule for the same directory, you would need to have a separate folder for each IE. have a folder called "show" and have the htaccess file that contains the rewrite data for show.
.htaccess for show folder:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)\.html$ /show.php?id=$1 [L]
</IfModule>
Then have another folder called "producer" with the 2nd rewrite rule.
.htaccess for producer folder:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)\.html$ /producer.php?id=$1 [L]
</IfModule>

Related

Multiple htaccess rewrite conditions and rules?

Having some issues when adding a new condition and rule to my htaccess file. We are needing to redirect traffic from a specific referral domain. Existing htaccess looks like this:
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteBase /~bayour8/
RewriteCond %{REQUEST_FILENAME} .*\.(jpeg|jpg|gif|png)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . /~bayour8/public/404.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /~bayour8/index.php [L]
#suPHP_ConfigPath /home/bayour8/public_html
</IfModule>
I need to add something like this but it never processes it:
RewriteCond %{HTTP_REFERER} !^http://domainx.com [NC]
RewriteRule ^/?first-page.html$ http://www.domainy.com/pages/pagename/ [L]
Any assistance would be appreciated!

.htaccess don't go to folder if slash on end

I have site where
site.com/index.php?page=test (include file test.php from pages folder into index.php)
is replaced by site.com/test
Goal:
If I go to site.com/index.php?page=forum/new_enrty
I want ot make url site.com/forum/new-entry
Problem:
if I open site.com/forum/new-entry it directs me to forum directory to new-entry file directly and not include new-entry into index.php
So far I got this:
<IfModule mod_rewrite.c>
RewriteEngine on
#RewriteBase /cms/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^ index.php [L]
</IfModule>
Replace your code with this:
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+?)/?$ index.php?page=$1 [L,QSA]
Try this one:
RewriteRule ^([^/]*)$ /index.php?page=$1 [L]

htaccess not working due to lack of knowledge

So I want to send three variables (separated by slashes) to my index.php but I can't get it working, I'm a leek with htaccess.
My base url is localhost:8888/smbo/
And I have this code which does not work:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /smbo/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([A-Za-z0-9_-]+)/([A-Za-z0-9_-]+)/([A-Za-z0-9_-]+)$ index.php?id=$1&idTwo=$2&idThree=$3
</IfModule>
Who can help me?
Try this code :
RewriteEngine On
RewriteBase /smbo/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/([^/]+)$ /index.php?id=$1&idTwo=$2&idThree=$3

My .htaccess pretty url not working which is in localhost (local machine with xampp)

I need to rewrite this url :
http://localhost/blog/post.php?id=48
into
http://localhost/blog/post/48
which is in localhost of my xampp.
I have created .htaccess file and wrote the below code to work.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^post/([0-9]+)/?$ post.php?id=$1 [NC,L]
</IfModule>
I have tried redirection and all other stuff. Everything is working but my rewrite rule alone not working. I've searched and tried all options but i couldn't succeed. Plz anyone help me out on this!!
Finally its working with below code:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /blog/ # This is the thing which made me struck with this
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^post/([0-9]+)/?$ post.php?id=$1 [NC,QSA]
</IfModule>
The ^post is your problem, this implies that post is the first part of your path, which it's not - blog is. Have you tried ^blog/post...:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^blog/post/([0-9]+)/?$ blog/post.php?id=$1 [NC,L]
</IfModule>

Conditional rewriting in htaccess file

I'm rather new to the whole .htaccess thing and I'm using the following right now to use 'pretty url's':
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?path=$1 [NS,L]
</IfModule>
Now i found my website a bit slow and decided to start gzipping my CSS files thru a php script I found somewhere on the web (the website). For this to work I need to rewrite the url to open the correct php file. That would look something like this:
RewriteRule ^(.*).css$ /csszip.php?file=$1.css [L]
But I only want the first to happen when the second doesn't and vice versa. In other words i'd like something like this:
<IfModule mod_rewrite.c>
RewriteEngine On
if request doesn't contain .css do
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?path=$1 [NS,L]
else do
RewriteRule ^(.*).css$ /csszip.php?file=$1.css [L]
</IfModule>
Can anyone help me with the proper code or a place where i can find a way to use some kind of conditional statement in htaccess files?
Thanks in Advance!:)
Try this
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} !.css
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?path=$1 [NS,L]
RewriteRule ^(.*).css$ /csszip.php?file=$1.css [L]
</IfModule>
Also, you can put the last rule on top

Resources