.htaccess 301 permanent redirect rule - apache

I have this url structure right now.
http://example.com/weather/in-city_name
which I want to permanently redirect to
http://example.com/city_name/weather
Here is what I'm writing to the .htaccess file using this reference
RedirectMatch ^/weather/(.*)$ http://example.com/$1/weather
But this doesn't work.
In my condition city_name is dynamic and manual entry is not possible.
Any suggestion about how to achieve the desire result would be great.

You can use this .htaccess:
RewriteEngine on
RewriteRule ^weather/in-(.+)/?$ /$1/weather [R=301,NC,L]

Related

Redirect 301 with percentage mark

I'm trying to perform the following redirection in .htaccess file, but I can not make them work. Can you help me?
Redirect is this:
https://www.example.com/example/CL%C3%81SULA%20DE%20MUERTE%20ACCIDENTAL%20CAD220130535.pdf to https://www.example.com/example-cl/images/example.pdf
I tried this:
Redirect 301 "/example/CL%C3%81SULA%20DE%20MUERTE%20ACCIDENTAL%20CAD220130535.pdf" https://www.example.com/example-cl/images/example.pdf
Thank you very much. Regards!
You can use this rule in site root .htaccess (a level above /example/):
RewriteEngine On
RewriteRule ^example/CL\xC3\x81SULA\x20DE\x20MUERTE\x20ACCIDENTAL\x20CAD220130535\.pdf$ /example-cl/images/example.pdf [L,R=302]
This is assuming /example/.htaccess doesn't exist. If you already have /example/.htaccess then use this rule as very first rule:
RewriteEngine On
RewriteRule ^CL\xC3\x81SULA\x20DE\x20MUERTE\x20ACCIDENTAL\x20CAD220130535\.pdf$ /example-cl/images/example.pdf [L,R=302]
I answer to myself.
I was making other redirections and find a good method to use in other similar redirections.
RewriteRule "^example/CL(.*)USULA DE MUERTE ACCIDENTAL CAD220130535.pdf" https://www.example.com/example-cl/images/example.pdf
With this method, this redirection works perfect.

Apache 301 bulk redirect

Is there a way to redirect all link with certain path? For example:
mydomain.com/free-products/product1
mydomain.com/free-products/product2
mydomain.com/free-products/productn
become:
mydomain.com/free/product1
mydomain.com/free/product2
mydomain.com/free/productn
I prefer to redirect using apache vhost instead of htaccess
Thanks for the clues..
Appreciated..
This should work
RewriteEngine on
RewriteRule ^free-products/product([0-9]+)$ /free/product$1 [R=301,L]
There is no matter where to place it, inside vhost or htaccess.
You can also use RedirectMatch.
RedirectMatch permanent ^/free-products/(.*) mydomain.com/free/$1
See http://httpd.apache.org/docs/2.2/mod/mod_alias.html for more information regarding mod_alias.

htaccess redirect catchall

I need to redirect a directory, and all subdirectories and files in that directory, to the same location (root). So anyone who tries to visit /old, /old/folder, /old/other-folder/xy/page.php, or anywhere else within the 'old' folder, should be redirected to the root domain.
So far, I have this:
Redirect 301 ^/old/.*$ /
Is this the best way of doing it, or would it be better to use (.*) instead of .*? What is the difference between the two?
Or - should I use a RewriteRule instead of a Redirect like above? If so, why?
Thank you!
The Redirect directive doesn't use regular expressions. It connects 2 path nodes together, which isn't exactly what you want. You can try using the RedirectMatch directive instead:
RedirectMatch 301 ^/old/ /
You can try this
RewriteEngine On
RewriteBase /
RewriteRule ^old/?(.*) /$1 [R=301,NC,L]
If you just want to redirect every old page to homepage/root(I'm not sure exactly what you want) than you can replace last rewriterule with
RewriteRule ^old/.* / [R=301,NC,L]

Using mod_rewrite correctly on Debian server

the good old mod_rewrite. I can't seem to get it right.
Typical scenario: A user types in "http://domain.com/page"
I want that the user is being redirected to "http://domain.com/page/page2"
My htaccess file looks as follows:
RewriteEngine on
RewriteBase /var/www/
RewriteRule ^/page/$ page/page2
RewriteRule ^/bla/$ page/page2/bla
The first rewrite rule works, the second on the other hand doesn't seem to have any effect. Any idea? Maybe a better way to do this?
And another question:
As I said the first rewrite works just fine, but the url is not pretty. "http://domain.com/page" changes to "http://domain.com/page/page2". Is there a way to keep the typed in url but still forward the user to the actual link?
I presume the .htaccess is in your DocumentRoot.
How does your /bla containing look like? This should not rewrite the URL in the browser.
Use this:
RewriteEngine on
RewriteBase /
RewriteRule ^(/?)page/?$ $1page/page2 [L]
RewriteRule ^(/?)bla/?$ $1page/page2/bla [L]

Redirecting URLs (with specific GET parameters)

I have this old survey link that is has been superseded by another link, so basically I want anyone trying to access the URL:
http://mywebsite.com/survey/view_survey.php?surveyID=1
To be redirected to:
http://mywebsite.com/survey/view_survey.php?surveyID=2
Can I do this in the Apache configuration or htaccess file?
I tried the following rule in the Redirect section of my httpd.conf file:
Redirect 301 /survey/view_survey.php?surveyID=1 http://mywebsite.com/survey/view_survey.php?surveyID=2
But it doesn't work. I am suspecting that the GET parameters are not used when processing the rule.
Is my only option to hack my code to redirect on a specific surveyID?
Following the suggestion of using the Rewrite rules, I tried the following in my .htaccess file:
RewriteRule ^survey/view_survey\.php\?surveyID=1525$ /survey/view_survey.php?sur
veyID=1607
But that doesn't work. I do have the rewrite engine up and running, because I have another rewrite rule currently running.
Try this in a .htaccess file:
RewriteEngine on
RewriteCond %{QUERY_STRING} (^|.*&)surveyID=1525(&.*|$)
RewriteRule ^survey/view_survey\.php$ /survey/view_survey.php?%1surveyID=1607%2 [L,R=301]
RewriteEngine On
RewriteCond %{QUERY_STRING} ^surveyID=1525$
RewriteRule ^/survey/view_survey\.php /survey/view_survey.php?surveyID=1607 [R=301]
Check out the QSA portion of the mod_rewrite.
It does GET string manipulation.
There might be a possible duplicate of this question and it is solved if this solution doesnt work for you:
Apache Redirect 301 fails when using GET parameters, such as ?blah=