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]
Related
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
I have the following file:
/forums/faq.php -- at http://www.domain.com/forums/faq.php
I would like calls to this file (which are usually done in the form of faq.php?faq=faqname) to be rewritten as:
/faq/faqname -- at http://www.domain.com/faq/faqname
I've been trying the following .htaccess rules to no avail. On my root .htaccess:
RewriteRule ^forums/faq.php?faq=(.*)$ faq/$1 [L,R=301]
RewriteRule ^faq$ faq/ [L,QSA,R=301]
On my /forums/.htaccess:
RewriteRule ^faq.php?faq=(.*)$ http://www.domain.com/faq/$1 [L,R=301]
Could I be overlooking something really obvious here? Is my syntax off?
You can ditch the rules in the /forums/ htaccess, the rules there have precedence over rules in the document root. You don't need them in both places.
Your rules will cause a redirect loop if they did what you wanted.
You can't match against the query string in a RewriteRule, so the first rule won't match anything. What you really want to do is match against the actual request.
# redirect /forums/faq.php?faq=faqname to /faq/faqname
RewriteCond %{THE_REQUEST} \ /forums/faq\.php\?faq=([^&\ ]+)
RewriteRule ^forums/faq.php$ /faq/%1? [L,R=301]
Then you need to internally change it back
RewriteRule ^faq/(.+)$ /forums/faq.php?faq=$1 [L,QSA]
I have the following problem. The link format below no longer exist:
photos.domain.com/web/poppic.php?n=[any number from 0 to the roof]
Ej,: photos.domain.com/web/poppic.php?n=30
I replaced it, a year ago, to just: photos.domain.com/
Years after, I am still having tons of 404 errors.
I need a permanent redirect, and an htaccess difective for search engines to do not follow and to no longer index that old link.
I tried rewriterule ^web\/poppic\.php?n=30 "http\:\/\/photos\.domain\.com" [R=301,L] will work. HOWEVER, I requires to write each line, from 0 to 9999999999999999999999999:
rewriterule ^web\/poppic\.php?n=0 "http\:\/\/photos\.domain\.com" [R=301,L]
rewriterule ^web\/poppic\.php?n=1 "http\:\/\/photos\.domain\.com" [R=301,L]
rewriterule ^web\/poppic\.php?n=2 "http\:\/\/photos\.domain\.com" [R=301,L]
etc.
How can I do it with a variable, to replace php?n=[number] for php?n=$variable (Or something like that)?
Also, it is not telling crawlers to do not follow/index the page.
Could you please help?
You can't match against the query string inside of a rewrite rule using apache's mod_rewrite. You need to use a rewrite condition and match against the %{QUERY_STRING} variable:
RewriteCond %{QUERY_STRING} ^n=[0-9]+
RewriteRule ^web/poppic\.php$ /? [L,R=301]
Not sure how your original rule:
rewriterule ^web\/poppic\.php?n=30 "http\:\/\/photos\.domain\.com" [R=301,L]
ever worked. It doesn't work for me under apache 2.2 or 2.4.
The beginning of .htaccess is
RewriteEngine On
#begin of rules for administration folder, redirect to https, if not https
RewriteCond %{REQUEST_URI} ^/administration
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R,L=301]
#end of rules for administration folder
#===no rules for /administration folder below this line===
#the rest part of .htaccess
Why does the rest part of .htaccess still performs for https://www.mydomain.com/administration/index.php ? How to stop the performing of the rest part of .htaccess file for urls that follow to administration folder? What's wrong in my code?
Thank you.
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
Add this line on top of your .htaccess file:
RewriteRule ^administration - [NC,L]
I had simmilar issue these days and I got a really interesting answer from the user DaveRandom in the PHP chat:
"Basically, mod_rewrite effectively runs in a loop (this isn't really true but effectively that's what it does) and the [L] flag is like a "continue" statement in PHP (it stops processing the rules below in the current iteration but it will then start processing the rules again from the top). So in your rules, the first iteration was matching the first rule, and then the second iteration skipped the first rule (it produced the same output as the input) and then matched the second rule."
Possible Solution: "if you are using apache 2.4 using [END] instead of [L] would be a fix".
It is because you have syntax error. .htaccess will ignore erroring lines
Change this:-
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R,L=301]
To this:-
RewriteRule .? https://%{HTTP_HOST}%{REQUEST_URI} [R=301]
I am trying to rewrite all the old oscommerce links to a new website. But I am having trouble with part of the URL I need to rewrite.
The link looks like this:
http://www.domain.com/product_info.php?cPath=3_72&products_id=129&osCsid=6j3iabkldjcmgi3s1344lk1285
This rewrite works for the above link:
RewriteCond %{REQUEST_URI} ^/product_info\.php$
RewriteCond %{QUERY_STRING} ^cPath=3_72&products_id=129&osCsid=([A-Za-z0-9-_]+)$
RewriteRule ^(.*)$ http://www.domain.com/apple/air.html? [R=301,L]
But will not work for:
http://www.domain.com/product_info.php?cPath=3_72&products_id=129
My problem is that I want the rewrite to work no matter if the &osCsid=6j3iabkldjcmgi3s1344lk1285 part is included or not.
I think you can achieve this by not specifying the closing delimiter ($)
Give this a try:
RewriteCond %{REQUEST_URI} ^/product_info\.php$
RewriteCond %{QUERY_STRING} ^cPath=3_72&products_id=129
RewriteRule ^(.*)$ http://www.domain.com/apple/air.html? [R=301,L]
By not putting the $ at the end of the regex string you are basically saying: match any string that starts with ..., no matter what comes after
Hope this helps :)
This should do the job just fine:
RewriteCond %{QUERY_STRING} ^cPath=3_72&products_id=129
RewriteRule ^product_info\.php$ http://www.domain.com/apple/air.html? [R=301,L]
There is no need for separate condition RewriteCond %{REQUEST_URI} ^/product_info\.php$ -- this part can be (actually, SHOULD BE, for better performance) moved to RewriteRule.
This is enough ^cPath=3_72&products_id=129 -- it tells "When query strings STARTS with ...". No need to include optional/non-important parameters osCsid=([A-Za-z0-9-_]+).
This rule is to be placed in .htaccess file in website root folder. If placed elsewhere some small tweaking may be required.