mod_rewrite with two parameters - .htaccess

Im getting a headache over this.
If the requested css file has qp defined it should rewrite as line 1. But if it doesn't it should rewrite as line 2.
Why isn't this working?
RewriteRule /css/(.*).css?qp=(.*)$ /build/css.php?request=$1&qp=$2 [QSA]
RewriteRule /css/(.*).css$ /build/css.php?request=$1 [QSA]
Can anyone explain why this isn't working?

Remove leading slash from RewriteRule. It doesn't receive it. And doesn't receive Query string. Ifqp is present, QSA will save it. Must be enough:
RewriteEngine on
RewriteRule ^css/(.*).css$ /build/css.php?request=$1 [QSA]

Related

mod_rewrite substitution not working

I'm having trouble with an Apache mod_rewrite. My .htaccess file is located in "/posts". Here's the contents:
RewriteEngine on
RewriteBase /
RewriteRule ^.*\.html$ ?url=/posts/2014/1215A.html [QSA]
The incoming request is for "/posts/2014/1214A.html". I want that request to be rewritten so as to be for, "/?url=/posts/2014/1215A.html". It appears that the regular expression is matched. The problem seems to be with the substitution. I've actually had the whole thing working; but, I must have somehow messed something up. Can anyone please tell me how to fix this? Thanks.
... doug
This should work :
RewriteEngine on
RewriteBase /posts/
RewriteCond %{REQUEST_URI} !^/posts/index
RewriteRule ^.*\.html$ ?url=/posts/2014/1215A.html [QSA]

.htaccess url rewriting without collision

I've got the following code in my htaccess
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ profile.php?username=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ profile.php?username=$1
The above code works fine like this, http://domain.com/username instead of http://domain.com/profile.php?username=username
I need a similar one but with a fake string like this
http://domain.com/gallery/username
The file is at http://domain.com/gallery.php
How do I achieve this without colliding with profile code?
Almost the same:
RewriteRule ^gallery/([a-zA-Z0-9_-]+)/?$ gallery.php?username=$1
If you also would allow other non ascii nicknames you should replace ([a-zA-Z0-9_-]+) by ([^\/]+).
Just as a hint you can remove the first rule if you add a questionmark after the slash that makes the slash optional.

htaccess redirect with parameters not working

I'm struggling with an Apache rewriterule. I need to do the following:
Redirect permanently:
http://domain.com/folder/viewer/data/settings.xml?prevent_cache=4760
to
http://domain.com/siteid/includes/themes/siteid/swfs/viewer/data/settings.xml?prevent_cache=4760
I've got the code below, it works without the url parameters but I can't seem to get it to work with parameters. Am i missing something?
RewriteCond %{QUERY_STRING} ^prevent_cache=([0-9]*)$
RewriteRule ^/folder/viewer/data/settings.xml$ http://domain.com/siteid/includes/themes/siteid/swfs/viewer/data/settings.xml [R=301,L]
Cheers
Shaun
The only error I can see, is the leading slash / in the RewriteRule pattern. This should be
RewriteCond %{QUERY_STRING} ^prevent_cache=[0-9]*$
RewriteRule ^folder/viewer/data/settings.xml$ /siteid/includes/themes/siteid/swfs/viewer/data/settings.xml [R,L]
You don't need to append the query string to the substitution URL, because this is done autmoatically.
When everything works as you expect, you can change R to R=301. Never test with 301 enabled, see this answer Tips for debugging .htaccess rewrite rules for details.
I can. Here are the rewrite condition and rule that you're looking for:
# once per htaccess file
RewriteEngine on
RewriteCond %{QUERY_STRING} prevent_cache=([0-9]*)
RewriteRule ^folder/viewer/data/settings.xml http://domain.com/siteid/includes/themes/siteid/swfs/viewer/data/settings.xml?prevent_cache=%1 [R=301,L]
But please considered this answer about the [R=301] flag: https://stackoverflow.com/a/15999177/2007055

mod_rewrite remove query string on top of a rewrite rule

I have the following rewrite rule:
RewriteRule ^(.*)-task-(.*)\.html$ /index.php/task/name/$2\-task\-$1 [L]
When I tried to open:
/heru-task-number-1.html
It is working fine. HOwever, when there is a query string appended to it:
/heru-task-number-1.html?whatever=value
It is actually not calling the correct rewrite. Thus, I wonder how can I make sure so that both:
/heru-task-number-1.html
AND
/heru-task-number-1.html?whatever=value
are actually calling the same thing that is:
/index.php/task/name/$2\-task\-$1
I have tried to do this but to no avail.
RewriteRule ^(.*)-task-(.*)\.html\?(.*)$ /index.php/task/name/$2\-task\-$1 [L]
Thank you for your help or feedback on this.
This is fixed by inserting the following code at the top of htaccess:
RewriteCond %{QUERY_STRING} (^|&)fb_comment_id=
RewriteRule ^(.*)$ /$1? [L,R=301]
basically, what it does is that it will remove any extra query string that has fb_comment_id and redirect 301 to the one without query string.
Thank you #oddant and #Gerben for helping out!

mod_rewrite help can't get it to work

This is the first time I use mod_rewrite and I can't get it to work. I have a website with bands and their IDs. What I want:
a URL /bands/My_Band_id13/ should redirect to /bands/index.php?bandname=My_Band&bandID=13
What I have:
RewriteRule ^/bands/(.*)_id(.*)/$ /bands/index.php?bandname=$1&bandID=$2
What am I doing wrong?
try adding the 'qsappend|QSA' (query string append) rewrite flag to your rule, ie.
RewriteRule ^/bands/(.*)_id(.*)/$ /bands/index.php?bandname=$1&bandID=$2 [QSA]
UPDATE: also, try removing / outcommenting your RewriteBase /. if this doesn't work, neither, try moving your .htaccess file into the same directory your index.php is in and adapt the RewriteRule, eg.
RewriteRule ^(.*)_id(.*)/$ index.php?bandname=$1&bandID=$2 [QSA]

Resources