I am currently chewing on this problem: I am reworking a sort of CMS system, the result will be that URLs will look entire differently, but I want to save links and bookmarks, so I scrathced this .htaccess file together:
RewriteEngine on
# Rewrite old links to match with new URL syntax:
# universal rewrite
RewriteCond %{REQUEST_URI}~%{QUERY_STRING} ^(.*?\/*)? (viewuser|viewstory|reviews|news)\.php~(.+)$ [NC]
RewriteRule ^(.+).php$ %1?action=redirect&source=$1&%3 [R=301,L,NE]
And it works, http://example.com/news.php?action=newsstory&nid=51 actually becomes http://example.com/?action=redirect&source=news&action=newsstory&nid=51
But you can see the problem, a double action. Using 'action' is not the most inventive term, but it is what the script I need to feed is working with, and also what I am getting from the old one, so I need to either:
replace the second 'action' with anything else
or serialize/escape the entire part which is defined by %3 from, the RewriteCond
In case you are wondering, the question mark required the use of a RewriteCond, couldn't get it to work any other way, so any solution that gets rid of it is just as welcome.
Well, thanks for looking into and maybe even shedding a light onto things, I just made my way into the whole regex thing, but havewn't entirely figured how variables are passed from chained RewriteRule to another, maybe that would have been the way to got, but try and error didn't get me anywhere.
Try removing the NE flag and use a B:
RewriteCond %{REQUEST_URI}~%{QUERY_STRING} ^(.*?\/*)?(viewuser|viewstory|reviews|news)\.php~(.+)$ [NC]
RewriteRule ^(.+).php$ /?action=redirect&source=$1&%3 [R=301,L,B]
The only problem is that the %1 backreference will get encoded as well so I left that out. If you must have it there, you can try doing a two step rewrite:
RewriteCond %{REQUEST_URI}~%{QUERY_STRING} ^(.*?\/*)?(viewuser|viewstory|reviews|news)\.php~(.+)$ [NC]
RewriteRule ^(.+).php$ /special-rewrite-here?action=redirect&source=$1&%3 [L,B]
RewriteCond %{THE_REQUEST} ^[A-Z]+\ (.*?\/*)?(viewuser|viewstory|reviews|news)\.php~(.+)$ [NC]
RewriteRule ^special-rewrite-here$ /%1 [L,R=301]
Fixed with help from Jon Lin:
RewriteCond %{REQUEST_URI}~%{QUERY_STRING} ^(.*?\/*)? (viewuser|viewstory|reviews|news)\.php~(.+)$ [NC]
RewriteRule ^(.+).php$ /special-rewrite-here?action=redirect&source=$1&old_data=%3 [B]
RewriteCond %{REQUEST_URI}~%{QUERY_STRING} ^(.*?\/*)? (viewuser|viewstory|reviews|news)\.php~(.+)$ [NC]
RewriteRule ^/.+$ %1 [L,R=301]
I had to do the RewriteCond again to preserve %1, and had to modify the second RewriteRule slightly, but he sure got me on the right track, so: problem solved, the rest will take place in PHP, that should be no problem then.
Related
I want to redirect
https://www.example.com/signup?plan=basic to https://www.example.com/signup?plan=basic-monthly and https://www.example.com/signup?plan=pro to https://www.example.com/signup?plan=pro-monthly .
How can I achieve this using htaccess ?
There are many questions related to this here. But, couldn't find an answer for this specific scenario.
This is the code I tried and failed:
RewriteCond %{QUERY_STRING} (^|&)plan=pro(&|$)
RewriteRule ^signup /$0?plan=pro-monthly [R=301,L]
Also, while trying the same with "basic" instead of "pro", the word "basic" i shown in red color as if it is a keyword.
Could you please try following, written with shown samples. Please make sure you clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteCond %{QUERY_STRING} ^(plan=(?:basic|pro))$ [NC]
RewriteRule ^(signup)/?$ $1?%1-monthly [L]
2nd solution: Or you could try following too. Make sure you either put 1st solution rules OR this one at a time.
RewriteEngine ON
RewriteCond %{THE_REQUEST} \s/(signup)\?(plan=(?:basic|pro))\s [NC]
RewriteRule ^ %1?%2-monthly [L]
We have a compliance issue with a supplier as if you type the URL with incorrect capitals their brand is wrong. We are on a private apache server so can edit htaccess etc as needed.
The URL is /example/Brand-Name
We would like all the following variations to resolve back to /example/Brand-Name/ (as 301 redirects)
/example/BRAND-NAME/
/example/brand-name/
/example/Brand-name/
/example/brand-Name/
etc etc.
All the htaccess things I have tried have lead to infinite loops.
RewriteCond %{HTTP_HOST} /example/Brand-Name/$ [NC]
RewriteCond %{HTTP_HOST} !/example/Brand-Name/
RewriteRule ^(.*)$ /example/Brand-Name/$ [R=301,L]
Have you tried the following two things? Your NoCase statement as just brand-name (not spelled exactly as your correct version) and, switching the order of the rules.
RewriteCond %{HTTP_HOST} !^/example/Brand-Name/
RewriteCond %{HTTP_HOST} ^/example/brand-name/$ [NC]
RewriteRule ^(.*)$ /example/Brand-Name/$ [R=301,L]
I believe the way this works is like an AND statement in an IF. If Line1 AND Line2, then do rewrite rule. Try these adjustments and let me know. If that doesn't work I will do a bit of research for you.
Edit: A quick search and I see a lot of rewrites looking like this - !^/ - so perhaps you need to add the little roofy ^ guy after your !. I believe in rewrite this denotes the begging of where to match, and should probably be on the 2nd line as well.
Not sure how you got your rules to work at all, since your %{HTTP_HOST} var is never going to look like /example/Brand-Name/. You probably want:
RewriteCond %{REQUEST_URI} !^/example/Brand-Name/
RewriteCond %{REQUEST_URI} ^/example/brand-name/ [NC]
RewriteRule ^example/brand-name/(.*)$ /example/Brand-Name/$1 [L,NC,R=301]
I'm sure this has been asked before, but I have no idea what it would be called.
I have done a quick Google and search here on SO.
Perhaps it would be better asked, how do I redirect http://site.com/shop to a specified URL like http://othersite.com/page without it affecting http://site.com/shop/?q=test or any other $_GET's.
RewriteRule ^shop http://othersite.com/page [R=301,L]
Works fine apart from it also affects links like http://site.com/shop/?q=test which I want to work.
Cheers.
You can use mod_rewrite to match against the query string (or to see if there is no query string):
RewriteEngine On
# blank query string
RewriteCond %{QUERY_STRING} ^$
# check host
RewriteCond %{HTTP_HOST} site\.com$ [NC]
RewriteRule ^shop/?$ http://othersite.com/page [R=301,L]
I have some difficulties with such an easy problem.
On our site we've got a paginator, wich works with GET-variable "p" (p=1 - is the first page, p=2 - second and so on).
I do not want the script to consider the value 1 of this variable (p=1). So I've got the query string like:
http://www.mysite.ru/mypage/some_page2?p=1
and want to redirect user via htaccess to page:
http://www.mysite.ru/mypage/some_page2
It would be nice, if htaccess rule handle not only value "1", but also "","bla-bla-bla" like
http://www.mysite.ru/mypage/some_page2?p=$#^&*_not_right_value
http://www.mysite.ru/mypage/some_page2?p=
Thank you for your attention.
UPD:
The working solution for my case:
RewriteCond %{QUERY_STRING} ^p=1$
RewriteRule ^(.*)$ /$1? [R,L]
Query string manipulation via .htaccess can generally only be done via mod_rewrite. I haven't tested it, but the following should be close to what you need.
RewriteEngine on
RewriteCond %{QUERY_STRING} p=1 [OR]
RewriteCond %{QUERY_STRING} p=[^0-9]
RewriteRule (.*) http://www.mysite.ru/$1 [R, L]
The second condition simply tests that the p= is followed by a number. Once tested, the rule can be expanded to test that only a number exists after p (ie, right now p=2garbage would work) but it is better to start off with just the basics.
The working solution for my case:
RewriteCond %{QUERY_STRING} ^p=1$
RewriteRule ^(.*)$ /$1? [R,L]
I have a strange problem with mod_rewrite, the rules that are relevant here are:
RewriteRule ^(.*)\/igre\-(.*)\.php\?Page=([0-9]+)$ game.php?GameUrl=$2&Page=$3 [L]
RewriteRule ^(.*)\/igre\-(.*)\.php$ game.php?GameUrl=$2&Page=1 [L]
And a corresponding URL might look something like this:
example.com/miselne-igre/igre-shirk.php?Page=2
example.com/miselne-igre/igre-shirk.php
The problem is that the first rule has no effect. If I use the first URL from the example I always get 1 into the Page variable, which shows that the second rule is used.
So what's wrong with the first one? And why is the second rule even matching a URL with ".php?Page=XYZ" at the end, if I said that the URL ends with ".php"?
ps: other rules in the .htaccess file are working fine...
The query string is not part of the URI path that is being processed by the RewriteRule directive. You have to use the RewriteCond directive to process the query string.
RewriteCond %{QUERY_STRING} ^Page=[0-9]+$
RewriteRule ^[^/]+/igre-([^/]+)\.php$ game.php?GameUrl=$1&%0 [L]
RewriteRule ^[^/]+/igre-([^/]+)\.php$ game.php?GameUrl=$1&Page=1 [L]
But you can still simplify this by using the QSA flag (query string append):
RewriteRule ^[^/]+/igre-([^/]+)\.php$ game.php?GameUrl=$1 [L,QSA]
mod_rewrite is not using the query in it's rewriting process. Therefor you first RewriteRule is ignored. You could combine it with a RewriteCond (haven't tested it though) like so:
RewriteCond %{QUERY_STRING} Page=([0-9]+)
RewriteRule ^(.*)\/igre\-(.*)\.php\?Page=([0-9]+)$ game.php?GameUrl=$2 [L, qsappend]
# qsappend appends the original query, in this case (Page=xx)
Ah, like Gumbo said; you can also use %1 to back reference to the page numer.
Is it just me or are your arguments back-to-front?
Do you mean:
RewriteRule ^(.*)\/(.*)\-igre\.php\?Page=([0-9]+)$ game.php?GameUrl=$2&Page=$3 [L]
RewriteRule ^(.*)\/(.*)\-igre\.php$ game.php?GameUrl=$2&Page=1 [L]
You wanted to match miselne-igre not igre-miselne.
Obviously this doesn't address the main issue, but thought I'd throw that in.
Dom