htaccess rewrite help needed - .htaccess

I was trying to write a rewrite rule, but so far couldn't achieve what I want.
my site is in a subdirectory and the htaccess file is in that directory.
What I want is, if I go to http://example.com/site/ or http://example.com/site/index.phpit should rewrite to
http://example.com/site/index.php?key=2
The value of key won't change. It is fixed. I want it to load by default but hide from the url.
What I tried is,
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /site/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /site/index.php?key=2&$1 [L,QSA]
</IfModule>
it's not working.
but if I go to address http://example.com/site/anything then it works.
Can you please help me in this?
I don't understand htaccess very well.
Thanks in advance.

Have it this way:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /site/
RewriteRule ^(index\.php)?$ index.php?key=2 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?key=2&$1 [L,QSA]
</IfModule>

Related

ReWrite Rule to a URL Friendly Page and User Profile

I want to use two RewriteRules to redirect Pages and Users to the right link but I'm coundn't achieve what I want, maybe someone could help me...
I want to redirect any Url Friendly page from:
http://www.example.com/my-url-friendly-page
to
http://www.example.com/index.php?cod=my-url-friendly-page
AND User Profiles from:
http://www.example.com/profile/username
to
http://www.example.com/profile/index.php?user=username
This is my htaccess, the first rule is working ok but I couldn't fix the second one...
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?cod=$1
RewriteRule ^/profile/(.*)$ index.php?user=$1 [QSA,L]
</IfModule>
Can anyone help me with this ?
Reorder your rules like this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^profile/(.*)$ index.php?user=$1 [QSA,L,NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?cod=$1 [L,QSA]
</IfModule>

htaccess rewrite rule for multiple pages

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>

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>

How to redirect using .htaccess?

I need my site to redirect from this url:
mysite.com/old/dir/param1/param2/...
to
mysite.com/dir/param1/param2/...
I need to keep the params, just remove the "old" in the url.
How can I add this rule to my .htaccess?
Thanks!!!
Here's my .htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?/$1 [QSA,L]
RewriteRule ^old/dir/(.*) mysite.com/dir/$1 [R=301,L]
</IfModule>
The right rule for it was
and I had to place it before all the other rules. So my .htaccess looks like:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^old/dir/(.*) /dir/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?/$1 [QSA,L]
</IfModule>
Thanks anyway! :)
You could just write it like this.
Rediret 301 mysite.com/old/dir/param1/param2/(.*) mysite.com/dir/param1/param2/$1
If you're planning to redirect the entire directory of old/dir to dir/, then try this:
Redirect 301 mysite.com/old/dir/(.*) mysite.com/dir/$1
The next option you could try is, Url Rewriting.
Assuming that you have set your Rewrite Engine on and have set the Rewrite Base, add these lines:
RewriteRule ^old/dir/(.*) mysite.com/dir/$1 [R=301,L]

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