So here's what I have.
www.website.com/foo (pretty URL to use on marketing pieces)
www.website.com/foobar (URL that actually exists on site)
I can get www.website.com/foo working perfectly with this:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /foo [NC]
RewriteRule ^ http://www.website.com/redirected/url-goes-here/ [L,R=301]
But that makes the www.website.com/foobar URL go there as well.
I'm sure this is a regex issue and I just don't know the correct symbol to get things working properly, but how can I make /foo redirect properly without effecting /foobar ?
Thanks.
Try this instead:
RewriteCond %{REQUEST_URI} ^/foo$ [NC]
RewriteRule ^ http://www.website.com/redirected/url-goes-here/ [L,R=301]
REQUEST_URI will get rid of the extra request headers that THE_REQUEST has. Then you can match the beginning and end of the requested URL with ^ and $.
You don't need the RewriteCond. Just be specific with the RewriteRule pattern
RewriteRule ^foo$ http://www.website.com/redirected/url-goes-here/ [L,R]
See more about regular expression.
Never test with 301 enabled, see this answer Tips for debugging .htaccess rewrite rules for details.
Related
I have an site lets say https://example.com/ and I would like to redirect every url that doesn't begins with /something to https://example.com/something/
I'm using Apache 2.4.29 (hosting) and my .htaccess looks like this.
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/something(.*)$ [NC]
RewriteRule ^(.*)$ /something/ [R=302,NC,L]
My problem is that when I'm on homepage (/) or any other page I'm redirected to /something/ which is correct but when I'm on /something/ I'm still beeing redirected to /something/ and it loops until ERR_TOO_MANY_REDIRECTS error shows up.
Here is a link to htaccess tester which shows that this should work and should not redirect me to /something/ when I'm already here but it is not the case on my hosting.
I was following this and this question but without success.
With your shown attempts, please try following set of rules. Please place them on top of htaccess rules file in case you already have existing rules.
Please make sure to clear your browser cache before testing your URLs.
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/?$
RewriteCond %{THE_REQUEST} !something [NC]
RewriteRule ^ /something? [R=302,NC,L]
You can try this rule with THE_REQUEST variable:
RewriteEngine On
RewriteCond %{THE_REQUEST} !\s/something [NC]
RewriteRule ^ /something/ [R=302,L]
Make sure to test it after completely clearing browser cache.
THE_REQUEST variable represents original request received by Apache from your browser and it doesn't get overwritten after execution of other rewrite directives. Example value of this variable is GET /index.php?id=123 HTTP/1.1
This is super simple but it's driving me crazy! I have a website at http://example.org/ and a subdirectory at http://example.org/ccc/
I want to redirect anything outside of the /ccc/ directory to a different website.
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/ccc/?.*
RewriteRule ^(.*)$ https://new-website.com/$1 [L]
But this code doesn't work, it redirects the /ccc/ directory. According to my research and testing with this htaccess tester, it should not redirect because the RewriteCond is checking against /ccc with optional slash and other characters after it.
What is happening? Does this look correct?
Edit: This method from this answer is also not working, the CCC domain is being redirected:
RewriteEngine on
RewriteRule ^ccc index.php [L]
RewriteRule (.*) https://new-website.com/$1 [R=301,L]
PHP 5.4.45, Apache/2.2.31
Assuming ccc/ directory doesn't have a separate .htaccess, you may use this rule:
RewriteEngine on
RewriteCond %{THE_REQUEST} !\s/ccc[/?\s] [NC]
RewriteRule ^ https://new-website.com%{REQUEST_URI} [L,R=301,NE]
THE_REQUEST variable represents original request received by Apache from your browser and it doesn't get overwritten after execution of other rewrite directives. An example value of this variable is GET /index.php?id=123 HTTP/1.1
It looks like [L] isn't behaving normally and I'm guessing it's the old version of Apache (2.2.31) because these rules worked on a separate website. I found this solution which seemed to work for this case, the third line below:
RewriteEngine on
RewriteRule ^ccc/? index.php [L]
RewriteCond %{ENV:REDIRECT_STATUS} != 200
RewriteRule ^(.*)$ https://new-website.com/$1 [L]
Explanation from that question:
The problem is that once the [L] flag is processed, all the next RewriteRules are indeed ignored, however, the file gets processed AGAIN from the begin, now with the new url.
This magic Condition will not process the catch all if the file was already redirected.
domain com/?fwcc=1&fwcl=1&fwl=1
First of all, I do not know from where this URL came as I tried to search but no answer available, just a guess might be from facebook but I want to get rid of it as it is showing me as duplicate content.
I tried to implement 301 redirect but no success tried different solutions e.g
RewriteRule ^/? http://www.domain.com/? [L,R=301]
RewriteRule ^/?fwcc=1&fwcl=1&fwl=1$ http://www.domain.com/? [L,R=301]
And this solution but no success. Please suggest by the way I am using Drupal7.5
You cant match against querystring in rule's pattern. You need to match against the_request variable using a RewriteCond
RewriteEngine on
RewriteCond %{THE_REQUEST} /\?fwcc=1 [NC]
RewriteRule ^ /? [L,R]
I am trying to redirect
domain.com/page?user=something
to
domain.com/page/something
with
RewriteRule ^page?user=(\d[^/]+) /page/$1/ [R=301,L]
For some reason that's not working though. It seems to be just ignoring it even. When I go to domain.com/page?user=something nothing happens. mod_rewrite is enabled and all other rules are executing. I assume it could be due to the ? in the URL but I might be wrong?
You can use these 2 rules in your root .htaccess:
RewriteEngine On
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} /page(?:\.php)\?user=([^\s&]+) [NC]
RewriteRule ^ /page/%1? [R=302,L,NE]
# internal forward from pretty URL to actual one
RewriteRule ^page/([^/.]+)/?$ page?user=$1 [L,QSA,NC]
In my site when i go to mysite.com/account or mysite.com////////account the output is the same.
How can I avoid multiple slashes to be parsed?
Thanks.
I think the only place where that is visible within mod_rewrite is in %{THE_REQUEST}. It depends on what you want to do what kind of rule you need. If you want to display a 404-error, you can do this:
RewriteCond %{THE_REQUEST} ^(GET|POST)\ [/]{2,}
RewriteRule ^ - [R=404]
It probably makes more sense to simply redirect to the place with just one slash:
RewriteCond %{THE_REQUEST} ^(GET|POST)\ [/]{2,}
RewriteRule ^ %{REQUEST_URI} [R]
See the documentation for more information about mod_rewrite.