htaccess redirect with parameters not working - .htaccess

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

Related

Converting NGINX rule to .htaccess

I'm trying to convert the following NGINX rule:
location ~ "^/calendrier/[0-9]{4}" {
rewrite ^/calendrier/(.*)$ /calendar/$1;
}
to .htaccess. I tried:
RewriteCond ^/calendrier/[0-9]{4} [NC]
RewriteRule ^/calendrier/(.*)$ /calendar/$1 [QSA,L]
but it isn't working.
Please help.
Thanks
You need to review the mod_rewrite documentation before attempting to convert rules from other platforms. Reason being: it is essential that you understand exactly how mod_rewrite should be used so that conversions are painless.
The problem with your conversion is that the RewriteCond is not checking that pattern against anything. Essentially, you've just done guess-work to see if it does what you want.
You only need to place the following in your /.htaccess file:
RewriteEngine on
RewriteRule ^calendier/([0-9]{4})/?$ /calendar/$1 [L]
The first part of the rule checks for calendier/<some_number> with an optional trailing slash (If you do not want the slash, you can remove /?).
This is the rewrite I used to get the job done.
RewriteCond %{REQUEST_URI} ^/calendrier/ [NC]
RewriteRule ^calendrier/(.*)$ /calendar/$1 [NC,L]

mod_rewrite and redirect causing loop

I have problem when I try to redirect and rewrite together.
I have site example.com/show_table.php?table=12 (max 99 tables). I wanted nice links, so I got this .htacces rw rule:
RewriteRule ^table/([0-9]{1,2})$ show_table.php?table=$1 [L,NC]
Now are links something like example.com/table/12 - it's definitely OK. But I want all old links redirect to new format. So I use Redirect 301, I added to .htaccess this code:
RewriteCond %{REQUEST_URI} show_table.php
RewriteCond %{QUERY_STRING} ^table=([0-9]{1,2})$
RewriteRule ^show_table\.php$ http://example.com/table/%1? [L,R=301,NC]
But when I visit example.com/show_table.php?table=12, I receive just redir-loop. I don't understant - the first is rewrite, the second is redirection, there ain't no two redirections. Do You see any error?
Thanks!
Instead of checking REQUEST_URI in the condition, you need to be checking in THE_REQUEST (which contains the full original HTTP request, like GET /show_table.php HTTP/1.1). When Apache performs the rewrite, it changes REQUEST_URI, so to the rewritten value, and that sends you into a loop.
# Match show_table.php in the input request
RewriteCond %{THE_REQUEST} /show_table\.php
RewriteCond %{QUERY_STRING} ^table=([0-9]{1,2})$
# Do a full redirection to the new URL
RewriteRule ^show_table\.php$ http://example.com/table/%1? [L,R=301,NC]
# Then apply the internal rewrite as you already have working
RewriteRule ^table/([0-9]{1,2})$ show_table.php?table=$1 [L,NC]
You could get more specific in the %{THE_REQUEST} condition, but it should be sufficient and not harmful to use show_table\.php as the expression.
You'll want to read over the notes on THE_REQUEST over at Apache's RewriteCond documentation.
Note: Technically, you can capture the query string in the same RewriteCond and reduce it to just one condition. This is a little shorter:
# THE_REQUEST will include the query string so you can get it here.
RewriteCond %{THE_REQUEST} /show_table\.php\?table=([0-9]{1,2})
RewriteRule ^show_table\.php$ http://example.com/table/%1? [L,R=301,NC]

link redirection using htaccess RewriteRule

I have these links in my website:
www.example.org/folder/files.php?file=folder/document.pdf
www.example.org/folder/files.php?force&file=2009.pdf
and I want redirect to :
www.example.org/files/folder/document.pdf
www.example.org/files/2009.pdf
I tried :
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^files/(.*)$ /files.php?file=$1 [R=301,L]
</IfModule>
but doesn't work!
any help?
RewriteRule ^files/(.*)$ /files.php?file=$1 [R=301,L]
There are two issues with this rule ... first, what you are matching needs to appear first in the rule, then what you are rewriting appears second - you have that backwards.
Once you reverse that, though, you run into the second issue - you can't match query strings in a RewriteRule, you need to match them in a RewriteCond:
To match www.example.org/folder/files.php?force&file=2009.pdf and redirect it to www.example.org/files/2009.pdf you would do:
RewriteCond %{QUERY_STRING} ^force&file=(.*)$ [NC]
RewriteRule ^folder/files.php$ /files/%1 [R=301, L]
The %1 matches what's in the parentheses in the RewriteCond.
Search on google first. The first thing displayed on google for htaccess is htaccess redirect. I think
Redirect /olddirectory/oldfile.html http://example.com/newdirectory/newfile.html (same line with a space) should work. Go to http://kb.mediatemple.net/questions/242/How+do+I+redirect+my+site+using+a+.htaccess+file%3F . Php would also do the work. Just goolgle things before asking them.

.htaccess php to html

sorry for this simple question, however i still cant get my head round using .htaccess
I'm trying to convert:
search.php?s=dvd&page=1
to
/Search/dvd/page1.html
Thanks,
Jack
I think something like:
RewriteRule ^search/([A-Za-z]+)/page([0-9]+)\.html$ search.php?$1&page$2
Should do the trick.
Further reading here: http://www.webforgers.net/mod-rewrite/mod-rewrite-syntax.php
you must put your link "/Search/dvd/page1.html" in the page and with htaccess it will convert to the "search.php?s=dvd&page=1" . i hope it be usefull :)
sample correct htaccess code :
RewriteEngine On
RewriteRule Search/(\d+)/page?(\d+)\.html$ search.php?s=$1&page=$2 [NC,L]
If I understand the question OP wants to convert php to html redirection. Try this rule in your .htaccess file:
RewriteEngine on
Options +FollowSymlinks -MultiViews
# to match /search.php?s=dvd&page=1
RewriteCond %{QUERY_STRING} ^s=([^&]*)&page=(.*)$ [NC]
RewriteRule ^search\.php/?$ /Search/%1/page%2.html? [R,L,NC]
# to match /search.php?page=12&s=dvd
RewriteCond %{QUERY_STRING} ^page=([^&]*)&s=(.*)$ [NC]
RewriteRule ^search\.php/?$ /Search/%2/page%1.html? [R,L,NC]
R=301 will redirect with https status 301
L will make last rule
NC is for no case comparison
%1 and %2 are the query parameter values
I think you want make clean URI, so if user type url like this:
search/televisi/page4.html
it same as like search.php?s=dvd&page=1
Add this code:
RewriteEngine On
RewriteRule ^search/([a-z]+)/page([1-9]+).html$ search.php?s=$1&page=$2

.htaccess redirect from GET variable to url string

I need to redirect
/search?keywords=somesearchterm
to
/search/somesearchterm
This seems incredibly basic but I've been pounding my head against it for an hour.
Thanks for taking the time to look at this.
You want to implement what is called a "301 Redirect" with mod_rewrite.
RewriteEngine ON
RewriteRule ^/search\?keywords=somesearchterm$ /search/somesearchterm
adding regular expressions:
RewriteEngine ON
RewriteRule ^/search\?keywords=(.+) /search/$1 [R=301,L]
R=301 means provide a 301 Header redirect so the user's URL changes in the browser, and L means don't process any more rewrite rules if this one matches.
If you want to do the reverse -- in other words, if someone goes to mysite.com/search/asearchterm and you want the URL to stay the same, but "behind the scenes" you want it to load a certain server script, do this:
RewriteEngine ON
RewriteRule ^/search/(.+) /search.php\?keywords=$1 [L]
You can not match aginst Query string in RewriteRule directive. Use %{THE_REQUEST} or %{QUERY_STRING} server variables to match the Query string :
The following rule works fine for this redirection
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/search\?kewords=([^&\s]+) [NC]
RewriteRule ^ /search/%1? [NE,NC,R,L]
RewriteRule ^search/([^/]+)/?$ /search?keyword=$1 [QSA,NC,L]

Resources