Apache mod_rewrite querystring problems - .htaccess

I have an old website - written in asp. The contact form uses a querystring to decide which form the visitor sees.
If a vistor wishes to just contact me, they may do so by clicking the link to:
http://example.com/contact.asp
If they wish to submit a fault - they do so by clicking the following link
http://example.com/contact.asp?problem=true&.......
Additional parameters are also passed after problem nb: ?problem=true&Customer... - however, I only need to know if problem=true
In the new site I have two pages: contact.html, and help.html
When a visitor goes to contact.asp they are redirected to contact.html - thanks to the following redirect rule:
RewriteRule ^contact\.asp$ http://example.com/contact-us.html [R=301,L]
However, I am having MASSIVE problems trying to get
http://example.com/contact.asp?problem=true to redirect to help.html
I've found tons of examples - but none of them work.. the page always redirects to contact.html
I have even commented out the above rewrite rule to see if my problem=true rule was being over-ridden.... This returned a 404. My problem=true rule just doesn't work.
I've been following examples such as: http://www.simonwhatley.co.uk/apache-htaccess-query-string-redirects
So to recap, I'd like to redirect contact.asp?problem=true to help.html, and contact.asp to contact.html
Many thanks in advance,
Rob

Not sure what you have tried as far as matching the query string, but the following should work.
RewriteCond %{QUERY_STRING} ^problem=true
RewriteRule ^contact\.asp$ http://primarysite.net/help.html [R=301,L]
RewriteRule ^contact\.asp$ http://primarysite.net/contact-us.html [R=301,L]
Hope that helps.

Related

Block specific url with ? with htacess

Firstly, I can't get the correct code looking at other post here, not working for me hahahaha.
I would like to block this specific url: /2017/06/wonder-womannuestra-princesa-feminista.html?m=1 (Related to an old blogger url)
Don't know why but different ips, without referrer and user agent, are spamming (not a real visit cause after a redirection to the new one on WordPress they visit all the links in the url) my site always entering by this url and I would like to block ONLY this one.
I've tried to redirect this one with Redirection plugin but I have a redirection to all "?m=(*)" and this one is the one working, not the one related to the specific url.
I just used : RewriteCond %{REQUEST_URI} ^/2017/06/wonder-womannuestra-princesa-feminista.html?m=1 but it doesn't work.
Could you help, I think the problem is that I'm not witting the correct code due to the "?" character. Many thanks.
With your shown samples, attempts considering that you need to block url(mentioned in comments/question), if this is the case then try following. This rule will forbid this specific url from being accessed.
Please these rules at top of your .htaccess file. Make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteCond %{THE_REQUEST} \s/2017/06/wonder-womannuestra-princesa-feminista\.html\?m=1 [NC]
RewriteRule ^ - [F,L]

Mod Rewrite rewrite URL weird behavior, keeps adding query string to displayed URL

I'm a beginner.
What I want
the request URL .../activity/1415/abcdefgh/ to be rewritten to .../activity/gallery.php?k=abcdefgh
but still display the URL to the visitor as .../activity/1415/abcdefgh/.
But when I try, the URL .../activity/1415/abcdefgh/ turned to .../activity/1415/abcdefgh/?k=abcdefgh.
The page displays fine, but I want to hide the query.
Basically what happened
Link says: http://localhost/activity/1516/iolfpqwx
Clicked link
Address bar says: http://localhost/activity/1516/iolfpqwx/?key=iolfpqwx, which I don't want.
My entire .htaccess in the activity/ directory:
RewriteEngine On
RewriteRule ^(\d{4})/(\w{8,})(?:/([\w\s]+)?)?$ gallery.php?key=$2 [L,QSA,NC]
My Specs:
XAMPP on Windows with Apache
What I know
I have not setup any redirect rules on my testing server, so I don't believe I have any [R] flags.
I have a similar .htaccess in an adjacent directory with the code RewriteRule ^settings/([^/]+)/*$ index.php?p=settings&s=$1 [L,QSA,NC] and it didn't show me query strings.
I've Googled my issue, but cannot find a solution. Please help.

redirect rule for multiple pages

I have page not found erros in webmaster because the page/2/0 part of a url is due to smart paging module clean url feature (Drupal) now i uninstalled the smart paging module but these page not found errors are still there.
www.mysite.com/a/b/c/page/2/0,
www.mysite.com/a/d/e/page/3/0,
www.mysite.com/a/f/g/page/4/0,
www.mysite.com/a/h/i/page/5/0
and so on.
I want to redirect
www.mysite.com/a/b/c/page/2/0 to www.mysite.com/a/b/c
www.mysite.com/a/d/e/page/3/0, to www.mysite.com/a/d/e
www.mysite.com/a/f/g/page/4/0, to www.mysite.com/a/f/g
www.mysite.com/a/h/i/page/5/0 to www.mysite.com/a/h/i
with one redirect rule. How to do this
in short i want to remove the page/x/0 part from the url and redirect it to the remaining part of that url.
You want something like this:
RewriteEngine On
RewriteRule ^/?([^/]+)/([^/]+)/([^/]+)/page/[0-9]+/0$ /$1/$2/$3 [L,R=301]
The literals here are after the first 3 path nodes: /anything/anything/anything/, then it must be followed by a page, then some numbers, then a zero. You can make it even more general, say after the "page", by changing the pattern to:
RewriteEngine On
RewriteRule ^/?([^/]+)/([^/]+)/([^/]+)/page/ /$1/$2/$3 [L,R=301]

.htaccess Rewrite Based on Existence of Path in URL

Here's the scenario, I have a website that used to be a static HTML site and WordPress blog using a subdomain (http://blog.domain.com).
I recently combined everything into a single WordPress installation. To maintain old links I had to rewrite requests like "http://blog.domain.com/index.php/2010/10/16/post-name" to "http://domain.com/index.php/2010/10/16/post-name". My problem is that when trying to visit just "http://blog.domain.com", I get redirected to "http://domain.com" when I want it to go to "http://domain.com/index.php/blog".
So, if a user requests "http://blog.domain.com" (by itself, with or without slash), I want it to go to "http://domain.com/index.php/blog". If they request an old URL of "http://blog.domain.com/some-link-to-a-post", I want it to redirect to "http://domain.com/some-link-to-a-post". In other words, if it's a URL to an actual post, I just want to strip the "blog" subdomain. If it's the old link to the main blog page, I want to remove the "blog" subdomain and append "/index.php/blog"
http://blog.domain.com/ -> http://domain.com/index.php/blog
http://blog.domain.com/index.php/2010/10/16/post-title -> http://domain.com/index.php/2010/10/16/post-title
Hopefully that's clear. I'm not an htaccess expert, so hopefully someone can help me out here. Thanks in advance!
Using the [L] command at the end of a rewrite will tell htaccess that this is the last rule it should match. If you put a rule to match your first condition at the top and the other rewrite rule you said you had already created after it, you should get your expected result.
Try this:
RewriteRule ^blog.domain.com(/?)$ domain.com/index.php/blog [L]
# Your other rewrite here #
I couldn't get that solution to work. However, I used the following:
RewriteCond %{HTTP_HOST} ^blog\.domain\.com$ [NC]
RewriteRule ^(.*)$ http://domain.com/index.php/blog/$1 [R=301,L]
That ends up in a URL like http://domain.com/index.php/blog/index.php/2010/06/04/post-title, but Wordpress is smart enough to fix it.

htaccess rewrite rule 'page numbers' problem

I've added a .htaccess file to my root folder for the purposes of rewriting dynamic URLs into static ones. This seems to have been done successfully but I am having problems with page numbers.
For example, if you were to visit a static page /widgets, the first page of the products is fine....but subsequent pages show up as /products.php?cat=27&pg=2 etc. What I want is for subsequent pages to be in the form of /widgets-pg2 or /widgets?pg=2.
Below is my rewrite rule that I used for the initial category page:-
RewriteRule ^widgets$ products.php?cat=27
If any of you experts can help with this, it would be much appreciated.
Are you expecting the cat to change as well? You'd need to account for that in your URL as well:
e.g. www.site.com/widgets/27/2 could be rewritten as:
RewriteRule ^widgets/([0-9]+)/([0-9]+)$ products.php?cat=$1&pg=$2
If widgets will always be cat 27 then you can change it to:
RewriteRule ^widgets$ products.php?cat=27 [QSA]
which is query string append
Try
RewriteRule ^widgets-pg(.+)$ products.php?cat=27&pg=$1
After that, go here :)
To allow a query string after your rewritten URL use the [QSA] flag:
RewriteRule ^widgets$ products.php?cat=27 [QSA]
A link would than be:
http://example.org/widgets?pg=167&perpage=100&tralala=Hi!
I tried the following but it resulted in a '404 Not Found' error when I go to /widgets:-
RewriteRule ^widgets-pg(.+)$ products.php?cat=27&pg=$1
And I tried the following:-
RewriteRule ^widgets$ products.php?cat=27 [QSA]
This worked correctly but to go to page two of the widgets page, I need to type in the browser:-
/widgets?pg=2
But the actual 'page 2' link still leads to:-
products.php?cat=27&pg=2
So apart from a rewrite rule....maybe I need a separate redirection rule? Or maybe need to change the page number links. But since these are dynamically generated, I'm not sure how to do this.
The following is the PHP code for the page:- http://freetexthost.com/3ubiydspzm

Resources