I want to redirect
www.example.com/mydomain?data1=value1&data2=value2
to
mydomain.example.com/?data1=value1&data2=value2
I want to redirect with the PHP variables passed to it.
How do I do that with the .htaccess file? I am sorry if this is answered elsewhere, I can't find it with search.
This is a straightforward rewrite, but...
...you need to know about the mod_rewrite Query String Append Flag ([QSA]):
The mod_rewrite rule to add to the root folder of www.example.com is:
RewriteEngine On
RewriteRule (.*) http://$1.example.com [R=301,QSA]
In your public_html/.htaccess, you can use :
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteRule ^mydomain/?$ http://mydomain.example.com/ [NC,QSA,L,R]
This will redirect
http://example.com/mydomain?query_strings
to
http://mydomain.example.com/?query_strings
Related
I'm trying to redirect a url but I can't seem to make it work for some reason. I'm trying to redirect my.domain.com/rss/abc/ to my.domain.com/rss/abc/?type=555
I've put this in my .htaccess
RewriteEngine On
# Redirects rss
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^/rss/abc/?$ /rss/abc/?type=555 [R=301,L]
Any pointers ? This is on a old TYPO3 site. Anyway it could hijack the request before doing the redirect ?
I can only recommend to use some kind of online htaccess rewrite rule checker, for example https://htaccess.madewithlove.com/ (not the only one, and no real preferance or recommendation about others).
Your rewrite rule is wrong, it should be:
RewriteEngine On
# Redirects rss
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^rss/abc/?$ /rss/abc/?type=555 [R=301,L]
Is it possible to create a rewrite code that includes a wildcard?
for example any request for:
http://example.com/something1/something2/*
would redirect to:
http://example.com/newthing
AND
will it matter where in the .htaccess file I place the rewrites?
This should work for you:
RewriteEngine On
RewriteRule ^something1/something2/(.*)$ /$1 [R=302,L]
It should change http://example.com/something1/something2/test to http://example.com/test
EDIT:
RewriteEngine On
RewriteRule ^shirts/(.*)$ /clothing.html [R=301,L]
I am trying to redirect using htaccess, but I do not know much about htaccess and redirection. What I want to do is:
redirect access to all subpages and directories e.g.
http://mydomain.com/index.php
http://mydomain.com/directory/
http://mydomain.com/new/directory/
to a specific URL on my domain say:
http://mydomain.com/one
I try to do it with
redirect /index.php http://mydomain.com/demo/
But my question is how to EXCLUDE specific URLs and directories from this redirection? How can I make rule to exclude specific pages from being redirected?
Any help is appreciated.
Your "tool of choice" should be RewriteCond
The RewriteCond directive can be used to filter more than just a URL, and each condition only applies to the next RewriteRule directive - you can "stack" RewriteCond directives, however.
Example:
RewriteCond %{REQUEST_URI} !^/path/to/non/redirect/url.php$
RewriteCond %{REQUEST_URI} !^/path/to/another/non/redirect/url.php$
RewriteRule ^$ http://mydomain.com/one
However, this can be simplified a litte:
RewriteCond %{REQUEST_URI} !^(/path/to/non/redirect/url.php|/path/to/another/non/redirect/url.php)$
RewriteRule /index.php http://mydomain.com/one
Apache Mod_Rewrite Documentation
I want to redirect /blog to /blog/. I have two condition if blog is in the url first condition will be called else second condition. Please anybody help me to do this.
I did this.
#blog
RewriteCond %{REQUEST_URI} ^/blog.*
RewriteRule (.*) /blog/$1 [L]
#squeezepage
RewriteEngine On
RewriteRule ^([0-9a-zA-Z]+)$ index.php?option=com_squeezepage&sqpage=$1 [NC]
Thanks.
You can do something like this to redirect /blog to /blog/ however if /blog is a physical folder then Apache does it automatically for you:
RewriteRule ^blog$ /blog/ [L,R=301,NC]
Do you only want the trailing slash for /blog requests or do you want it for all directories?
Because there is a directive called DirectorySlash on which might be what you're looking for.
See this question.
Damn you modrewrite
I have a website hosted at a url like:
http://mydomain/mocks/thesite/
Now I want to move it to a new domain
http://thesitesdomain.com/
My htaccess looks like this:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.thesitesdomain\.com
RewriteRule (.*) http://www.thesitesdomain.com/$1 [R=301,L]
Now this works fine as long as there is something after /mocks/thesite/. eg: http://mydomain/mocks/thesite/index.html redirects to http://www.thesitesdomain.com/index.php.
However the problem is that:
http://mydomain/mocks/thesite/ redirects to http://thesitesdomain.com/mocks/thesite/. Any idea why? How to stop this?
The .htaccess file is in the root of /mocks/thesite/ (if that helps)
Thank you
You should try to use the variable REQUEST_URI you might have a little more success with that. It should be the request uri and file name. To
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.thesitesdomain\.com
RewriteRule .* http://www.thesitesdomain.com/%{REQUEST_URI} [R=301,L]
I can't remember but to also redirect with the query string (get variables) I think you need to add it like this.
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.thesitesdomain\.com
RewriteRule .* http://www.thesitesdomain.com/%{REQUEST_URI}?%{QUERY_STRING} [R=301,L]
Been a while since really doing a domain redirect....
BTW this is a good read on htacces configuration:
http://corz.org/serv/tricks/htaccess2.php