htaccess rewriterule precise (exact) match - .htaccess

I have a website on which I would like to redirect the page :
website.com/component/users/profile
to
website.com/redirect
However, I would like to allow to navigate to :
website.com/component/users/profile?layout=edit
The "?" seems to cause a problem. I found many solutions for doing this the other way around (go from link with "?" to a page without a "?").
What I have right now allows me to navigate to :
website.com/component/users/profile
but not to
website.com/component/users/profile?layout=edit (this is the one getting redirected)
I am trying to achieve the exact opposite.
The code in my htaccess file is :
RewriteCond %{QUERY_STRING} ^component/users/profile$
RewriteRule ^$ /redirect [L,R=301]
Thanks!

Related

Redirect and keep the parameter in the url on .htaccess

It should probably be an easy setup, but without familiarity with the terms used, I can't come up with a solution. I tried several examples that I found on the internet.
I just wanted that when the user accessed the site via a url https://example.com?foo=bar he would be directed to https://www.example.com/new-nice-page?foo=bar
Changing the page and keeping the parameter.
The last thing I tested:
RewriteEngine on
RewriteRule ^https://www.example.com/new-nice-page?foo=$ example.com?foo=$1 [QSA]
You may use this redirect rule at the top of .htaccess:
RewriteEngine On
RewriteCond %{QUERY_STRING} (^|&)foo= [NC]
RewriteRule ^/?$ /new-nice-page [L,R=301]
Note that query string is automatically carried forward to target URL.
Looking at this thread : simple .htaccess redirect : how to redirect with parameters?
you simply redirect , without worying about the parameters, because they are automatically passed :
Redirect permanent /jsn.php http://www.site2.com/jsn.php

htaccess redirect of different add-on domains to folders which are associated to each domain

We tried various htaccess rewrite rules but could not get to work what we need to do. Maybe some advise here?
Assume we have a main domain (a.com) with 2 add-on domains (b.com and c.com), all pointing to the website root.
Then we have folders, all in root, like /folder_a1, /folder_a2, /folder_a3,/folder_b1,/folder_b2,/folder_b3 and /folder_c1, /folder_c2,/folder_c3 in which there are php files.
Users shall be able to come to the site via:
URL=a.com/folder_1/xxx.php and be redirected to root/folder_a1
URL=b.com/folder_1/xxx.php and be redirected to root/folder_b1
URL=c.com/folder_1/xxx.php and be redirected to root/folder_c1
Each time we want to keep in the browser address bar the URL the user came from (if he came via a.com we want to keep showing a.com... etc.)
In this example we basically have to map the url string .../folder_1, dependent on the URL used, either to folder_a1, folder_b1 or folder_c1.
We tried (amongst others):
RewriteCond %{HTTP_HOST} ^www\.a\.com [NC]
RewriteRule ^folder1(/.*|)$ /folder_a1$1 [L,NC]
RewriteCond %{HTTP_HOST} ^www\.b\.com [NC]
RewriteRule ^folder1(/.*|)$ /folder_b1$1 [L,NC]
RewriteCond %{HTTP_HOST} ^www\.c\.com [NC]
RewriteRule ^folder1(/.*|)$ /folder_c1$1 [L,NC]
But that does not do the trick. With these rules we always and up at folder_a1.
Any suggestions how we can do this?
We found the solution (thanks to the support of our host rochenhost !).
As there was no answer here (yet ;), here is what we found:
The only issue was that we did not repeat the re-write condition before every re-write-rule, which meant that the condition was only applied once, to the first rule, and to none of the following rules.
Once added, all works.
Cheers

How to write a htaccess to change url but point to actual path internally using regex match

url = www.example.com/de/abc
I just want to change fix url word 'abc' into fix word 'xyz' for browser compatibility only changed url show in browser. But change url point to actual directory path means
www.example.com/de/xyz will follow internally www.example.com/de/abc.
I used some regex to do this, but not able to get actual solution
How can I do this.
Thanks in advance
Code from comment :
RewriteCond %{REQUEST_URI} ^/de/advertiseWithUs$ [NC]
RewriteRule ^(.*) /de/unsere-tipps [R=302,L]
If I have correctly understood what you want, here a solution you can try :
RewriteRule ^de/advertiseWithUs$ http://yourdomain.com/de/unsere-tipps [R=302,NC,L]
RewriteRule ^de/unsere-tipps$ de/advertiseWithUs [NC,L]
The first rule change URL (with a 302 redirection), the second one make the internal redirection to point to the right page.

htacces - need to fix broken links coming from other sites to mine

I am having an issue where Google Webmaster Tools is reporting a ton of 404 links to my site which are coming from ask.com.
I have tried to get ask.com to fix their side but of course they are not, so now I am stuck with over 11k of bad links to my site which I am suspecting is effecting my ranks right now.
Anyways I have a possible way to 301 them, but not sure how to do it with .htaccess.
Here is the bad link pointing to my site
http://www.freescrabbledictionary.com/sentence-examples/fere-film/feverous/about.php
It should be
http://www.freescrabbledictionary.com/sentence-examples/fere-film/feverous/
Besides the about.php there are other variations of endings as well, I basically need to be able to remove the ending.
Problem is that the URL after /sentence-examples/ can change. The beginning is always:
http://www.freescrabbledictionary.com/sentence-examples/
So basically:
http://www.freescrabbledictionary.com/sentence-examples/<-keep but can change->/<-keep but can change->/<-remove this->
This .htaccess should be placed on the folder before sentence-examples:
RewriteEngine on
# Redirect /sentence-examples/anything/anything/remove to /sentence-examples/anything/anything/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(sentence-examples/[^/]+/[^/]+)/.* [NC]
RewriteRule ^ /%1/? [R=302,PT,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/(.*)$ /sentence-examples/examplesentence.php?havethis=$1&word=$2 [L]
Change 302 to 301 once you confirm it's working as expected.
If you have a CMS installed you might need a different rule to work along with it without conflicting.
Keep in mind that if you had previously tried different redirects using 301 aka permanent redirect its recommended that you use a different browser to test this rule to avoid the caching.
This is possibly quick and dirty but I've done a simple test on localhost and here just to make sure it works.
RewriteEngine On
RewriteRule ^sentence-examples/(.*)/(.*)/(.*)\.php http://www.freescrabbledictionary.com/sentence-examples/$1/$2/ [R=301,L]
You can see that I've added wildcard groups (.*) to the RewriteRule so that we can pick up the elements of the URL that we need to aid in proper redirection i.e. $1 and $2. You can also use the third one ($3) to get which destinations are being targeted alot for your SEO needs.
NB: The rule above assumes that that the redirected URL will always be from a .php target and to ensure that you can redirect regardless of whatever comes after the 3rd URL segment replace the RewriteRule with this
RewriteRule ^sentence-examples/(.*)/(.*)/(.*)$ http://www.freescrabbledictionary.com/sentence-examples/$1/$2/ [R=301,L]

Question about a redirect

I just noticed that sometimes (even when given a wrong url) load perfectly fine. How do they accomplish this? What I mean is, suppose you click on a link that seems good like www.foo.com but it contains in the end a space character which would appear on the address bar as www.foo.com%20 some sites manage to redirect this to their correct url while others just break. How can this be achieved? I'm guessing it's something to do with the .htaccess but I have no idea what to do or where to do it.
The URL I'd like to redirect looks like this actually: http://foo.com/%C2%A0
I get the following error message:
The requested URL /%C2%A0 was not found on this server.
How can I make this redirection?
So far I came up with:
RewriteEngine on
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /[^%?\ ]*\%
RewriteCond %{REQUEST_URI} !^/
RewriteRule ^(.*)$ http://www.foo.com/ [R=301,L]
but it's not working at all

Resources