Apache .htacces redirect - apache

I try to create redirect rules. I have two URLs www.example.com/speak and www.example.com/speaking. I added below lines to my .htaccess
RewriteRule speaking$ /?utm_source=offline&utm_medium=books&utm_campaign=bookmarks_uk [NC,QSA,R=301]
RewriteRule speak$ /?utm_source=offline&utm_medium=bookmarks&utm_campaign=english_course [NC,QSA,R=301,L]
but each time I'm redirected to /?utm_source=offline&utm_medium=books&utm_campaign=bookmarks_uk.
I swapped them around but it didn't work.
Any idea?

Those rules should work fine, but can be improved to cater for the trailing slash. Also use ^ to mark the beginning of the string and $ to mark the end of the string.
RewriteRule ^speaking/?$ /?utm_source=offline&utm_medium=books&utm_campaign=bookmarks_uk [NC,QSA,R=301]
RewriteRule ^speak/?$ /?utm_source=offline&utm_medium=bookmarks&utm_campaign=english_course [NC,QSA,R=301,L]
I suspect you problem is browser caching. Try clearing your browser cache and restarting your browser.

You are being redirected because you are using the R=301 flag. If you are trying to access /speaking and show the contents of /?utm_source=offline&utm_medium=books&utm_campaign=bookmarks_uk, then you need to remove the flag in question.
Your resulting rules should look like this:
RewriteRule ^speaking/?$ /?utm_source=offline&utm_medium=books&utm_campaign=bookmarks_uk [NC,QSA,L]
RewriteRule ^speak/?$ /?utm_source=offline&utm_medium=bookmarks&utm_campaign=english_course [NC,QSA,L]

Related

htaccess Rewrite rule from "example.com/de/foo" to "example.de/foo"

RewriteRules can be such a pain for me, I cannot get this one to work.
I have to redirect urls like example.com/de/anypath to example.de/anypath.
[anypath] can be really any path, as I have to get it work for
example.com/de/articles/programming/hello-world (would be redirected to example.de/articles/programming/hello-world)
as well as for example.com/de/events/pic-nic (would be redirected to example.de/events/pic-nic).
This is what I wrote so far :
RewriteRule "^/de/(.*)$" "http://example.de/$1" [R=301,NC,L]
I also tried with RewriteCond with no more luck
RewriteCond %{HTTP_HOST} http://example.com/de
I am working with xampp, but tested on my web server with same
result.
I know this .htaccess file is working (get error if I enter a
typo)
I got some result when testing with something like :
RewriteRule "^(.*)$" "https://google.com" [R=301,NC,L]
Any help would be appreciated !
RewriteRule "^/de/(.*)$" "http://example.de/$1" [R=301,NC,L]
In .htaccess, the URL-path matched by the RewriteRule pattern does not start with a slash. ie. It should be "^de/(.*)$", not "^/de/(.*)$".
You don't need the double quotes and the NC flag is probably redundant, unless you also need to match dE, Ed or DE.
For example (near the top of the root .htaccess file):
RewriteRule ^de/(.*) http://example.de/$1 [R=301,L]
(HTTP, not HTTPS?!)
The trailing $ on the RewriteRule pattern was also redundant.
Test first with 302 (temp) redirect to avoid potential caching issues.
RewriteCond %{HTTP_HOST} http://example.com/de
The Host HTTP request header (ie. the value of the HTTP_HOST server variable) contains the hostname only. eg. example.com only in your example.
Any server variable that is prefixed with HTTP_ refers to the HTTP request header of the same name.
I got some result when testing with something like :
RewriteRule "^(.*)$" "https://google.com" [R=301,NC,L]
Careful with testing 301s since they are cached persistently by the browser. You will need to clear your browser cache before testing!
I added two conditions (the first is to apply according to local or live site, the second to leave the node paths unchanged) :
RewriteCond %{HTTP_HOST} local.example.com
RewriteCond %{REQUEST_URI} !^.*/de/node/.*$
And it is working as expected. Thanks again.

Redirect from folder to root

I would like to move all content from /downloads/assets/ folder to /downloads/ folder.
How do I add redirect for /downloads/assets/{anystring} to /downloads/{anystring}?
Now I manually add every redirect like this:
RewriteRule ^downloads/assets/views?$ /downloads/views [L]
But it's a dream job. Can we use variables instead?
Spend some time with the RewriteRule documentation, as this is a very rudimentary usage. You will need to capture everything after assets/ in (.*) and rewrite it as $1.
RewriteEngine On
RewriteRule ^downloads/assets/(.*) downloads/$1 [L]
The above will perform a silent internal rewrite. If you need to redirect the browser rather than silently rewrite, use [L,R=301] instead of [L].

Simple RewriteRule for first directory

This is pretty basic but I can't find a solution that works. I just need to redirect any URLs from an old directory to a new URL.
Example:
/search/whatever
to
/jobs/search
I don't need to map the whatever, I want all traffic to a URL that begins /search to get redirected (301).
Using this:
RewriteRule /search /jobs [R=301,L]
Works but I have a URL within /jobs that also now gets redirected:
/jobs/search
And that's wrong - it needs to match the start of the URL. So I tried this:
RewriteRule ^/search /jobs [R=301,L]
But that doesn't redirect at all, so I'm stuck.
Another example would be this:
RewriteRule /careers-at-pure /emea/contact-us/careers-at-pure [R=301,L]
This creates a loop as careers-at-pure is in the old and new URLs, but the following doesn't get matched and redirected:
RewriteRule ^/careers-at-pure /emea/contact-us/careers-at-pure [R=301,L]
Any suggestions?
Thanks
The leading slash is removed from the URI when they're being put through rewrite rules in htaccess files. You need to remove the leading slash from the regex, or at least make it optional:
RewriteRule ^/?search /jobs [R=301,L]

htaccess rewrite of directory to variable fails on localhost

Let's take something like this question. So, I'm looking to rewrite:
http://www.example.com/test to
http://www.example.com/page.php?v=test
with:
RewriteRule ^(.*)$ page.php?v=$1 [L]
This takes me to the page http://www.example.com/test/?v=test
Why does it not stay on http://www.example.com/test without the trailing slash and the query string.
PS: I'm using WampServer on Windows
in RewriteRule don't put / and the begining!
RewriteRule ^(.*)$ page.php?id=$1 [L]
The problem arises if test is an existing directory on the server. Apache then uses the DirectorySlash Directive to "fix up" the URL pointing to the directory by adding a trailing slash.
I'm answering my own question here, but the solution (as the above link states) is to add DirectorySlash Off to .htaccess

Redirecting to same page with .htaccess

From my .htaccess file:
RewriteRule ^showPAGE.php page [NC,R=301]
RewriteRule ^page showPAGE.php [NC,L]
I want users going to url domain.com/showPAGE.php to be redirected to domain.com/page .
When domain.com/page is being entered, I want it to show the content of the file showPAGE.php.
Is that possible to do?
The above results an infinite redirection loop.
Thanks
You're trying to do something that's very tricky. The problem is that, by design, the RedirectRule directive always triggers again the complete set of rules. You can only get out of the loop when you obtain a final URL that does not match any of the rules and that's the tricky part since you are reusing the showPAGE.php name.
My best attempt so far involves adding a fake hidden string:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/showPAGE\.php
RewriteCond %{QUERY_STRING} !^internal
RewriteRule ^ http://%{HTTP_HOST}/page [NC,R=301,L]
RewriteRule ^page$ showPAGE.php?internal [NC,L]
It works but it's not pleasant. Definitively, it's easier to handle the redirection from with PHP or to simply pick another name.
The redirect from showPAGE.php to page needs to have [L] so that it will stop processing and redirect at once, rather than going on and applying other rules (which at once map it back to showPAGE.php). Try this:
RewriteRule ^showPAGE.php page [NC,R=301,L]
RewriteRule ^page showPAGE.php [NC,L]