simple url to url via RewriteRule in htaccess not working - apache

I need to redirect a single URL to another single URL, no parameters or anything
domain.com/snow to
superdomain.com/longer/url/snow
i am trying with, in .htaccess the following:
RewriteRule ^snow http://superdomain.com/longer/snow [R=301,L]
or
RewriteRule ^snow$ http://superdomain.com/longer/snow [R=301,L]
But none of them are working, any idea what am I missing?
Note: the url's are only examples but they have same structure than what i need

just found out! /snow/ flolder had its own .htacess :)

If you don't need parameters or anything, RedirectPermanent is a simpler option:
RedirectPermanent /snow http://superdomain.com/longer/snow

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 htaccess rewrite (Pretty URLs)

I have a sneaking suspicion this is not possible, but figured I would ask regardless.
Is it at all possible to take a URL passed to a server in the form of:
http://domain.com/index.php?Action=Controller/Action&one=1&two=2&three=3
And rewrite it to appear as:
http://domain.com/Controller/Action/1/2/3
I am trying to clean up an borderline ancient project to support "Pretty URLs" and I would really like to make the URLs display a bit nicer. I know I could setup a 301 header redirect to the new URL, but I would prefer to avoid that overhead if at all possible.
Any thoughts?
Thanks!
To get
http://domain.com/index.php?Action=Controller/Action&one=1&two=2&three=3
To appear as
http://domain.com/Controller/Action/1/2/3
You will need to use %{QUERY_STRING} to capture the query string data. Your .htaccess file will look like this:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^Action=Controller/Action&one=(\d+)&two=(\d+)&three=(\d+)
RewriteRule ^.+ /Controller/Action/%1/%2/%3 [R=301,L]
This will set up a permanent redirect to the new page. You can play around and test .htaccess rewrite rules here: htaccess.madewithlove.be
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteBase /
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ index.php?Action=$1/$2&one=$3&two=$4&three=$5 [L,QSA]

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]

.htaccess help, replacing URI segment

I have a URL that looks like this...
http://www.domain/a/var
I am hoping it would be possible to rewrite the URL using .htaccess so that /a/ would be replaced by /b/.
Is that possible, and if so, how?
RewriteEngine On
RewriteRule ^a/(.*) /b/$1
This should do that you need
RewriteEngine on
RewriteRule ^/a/(.*) /b/$1
Note: You'll need to add [PT] or similar if you need mod_alias or mod_index or similar to work using the new path.

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=