I'm trying to tweak some rewrites in an .htaccess file but my regexp/mod_rewrite knowledge is thin!
We have primarydomain.com and secondarydomain.co.uk, and we want to rewrite anything and everything to www.primarydomain.com
At present I'm doing this:
RewriteCond %{HTTP_HOST} ^secondarydomain.co.uk$ [OR]
RewriteCond %{HTTP_HOST} ^www.secondarydomain.co.uk$ [OR]
RewriteCond %{HTTP_HOST} ^primarydomain.com$
RewriteRule (.*)$ http://www.primarydomain.com/$1 [R=301,L]
.. but I'm sure there is a better way?? Is there a rule that would rewrite any host that's not www.primarydomain.com? I tried playing about with the not operator (!) and managed to cause a redirect loop so don't want to do that :/
Any help appreciated! Ben
Ok, Found it myself:
RewriteEngine On
RewriteRule ^(.*)$ http://www.primarydomain.com/ [R=301,L]
Related
Today I have
https://www.teste.com/app/assets/folderX/file.pdf
But I need to redirect to:
https://assets.teste.com/folderX/file.pdf
I got close to the solution with:
RewriteCond %{REQUEST_URI} ^/app/assets/ [NC]
RewriteRule ^(.*)$ https://assets.teste.com/$1 [R=301,L,QSA,NC]
But the rewrite ended like:
https://assets.teste.com/app/assets/folderX/file.pdf
On the ReWrite I need to remove the subfolder /app/assets/ from it.... but I have no idea how.
After some tries. could do it using:
RewriteCond %{REQUEST_URI} ^/app/assets/ [NC]
RewriteRule ^/?app/assets/(.*)$ https://assets.teste.com/$1 [R=301,L]
I nice tool to test is https://htaccess.madewithlove.be/
You may use this rule instead:
RewriteCond %{HTTP_HOST} ^(?:www\.)?(teste\.com)$ [NC]
RewriteRule ^(app)/assets/(.*)$ https://$1.%1/$2 [R=301,L,NE,NC]
Make sure to use a new browser to test this change or completely clear old browser cache.
I want to rewrite all addresses after this
http://www.mydomain.com/questions/*
to this
http://*.mydomain.com/
for example:
http://www.mydomain.com/questions/example
http://example.mydomain.com/
Can anyone help me in this issue!?
Thanks
There you go.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.mydomain\.com$ [OR]
RewriteCond %{HTTP_HOST} ^.mydomain\.com$
RewriteRule ^questions/([^/]+)/?$ http://$1.mydomain.com/ [NC,R=301,L]
Plug this into your .htaccess file and you should be good to go. :)
I would like to redirect all url requests with the phpnuke string modules.php?name=News&file=article&sid=XXX (xxx is a number) to my domain-name.eu/XXX . How is that possible?
i tried
RewriteCond %{QUERY_STRING} ^(.*&|)modules.php?name=News&file=article&sid=(&.*|)$
RewriteRule ^(.*)$ %1domain.eu/%2 [L,R=301]
but it has no effect. Where is the mistake?
Also i don't understand why a simple
RewriteRule ^(.*)$ /modules.php?name=News&file=article&sid=$1 [L,R=301]
doenst work. Any ideas?
Thanks a lot for any help.
Not sure why I'm seeing this in Drupal questions. But try this.
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/modules\.php$
RewriteCond %{QUERY_STRING} ^sid=([0-9]*)$
RewriteRule ^(.*)$ http://domain.eu/%1 [R=301,L]
You can't match everything after the sid=(.*) regex part in the URL in most cases. modules.php?name=News&file=article&sid=XXX and modules.php?sid=XXX&name=News&file=article are the same in functionality.
Learned at http://www.simonecarletti.com/blog/2009/01/apache-query-string-redirects/
I have a site that has two domains pointing to it, let's call them:
work.mysite.com
play.mysite.com
This is bad practice, so I want to choose work.mysite.com and make it the canonical URL, permanently redirecting play.mysite.com to it.
I'm in the root directory for these two domains, in a .htaccess file, banging my head against the cement floor and wishing I wasn't here. Here's what I am currently trying. Tell me how totally wrong I am, please?
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?play\.mysite\.com [NC]
RewriteRule ^/?(.*)?$ http://work.mysite.com/$1 [R=301]
</IfModule>
That gets me a really pretty 500 Internal Server Error. How far off am I?
RewriteCond %{HTTP_HOST} !work.example.com [NC]
RewriteRule ^(.*)$ http://work.example.com/$1 [R=301,L,QSA]
This will also remove the www from www.work.example.com
Not sure if the QSA is needed, but I think it will prevent play.example.com/?home from being redirecting to work.example.com/ instead of work.example.com/?home
Ah, I think I was just trying to be too fancy. This seems to work fine:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^play.mysite.com$
RewriteRule ^(.*)$ http://work.mysite.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^www.play.mysite.com$
RewriteRule ^(.*)$ http://work.mysite.com/$1 [R=301,L]
</IfModule>
This worked for me.
RewriteCond %{HTTP_HOST} ^jquery\.webcodehelpers\.com
RewriteRule (.*)$ http://uieiq.webcodehelpers.com/$1 [R=301,L]
I used this to redirect a subdomain to another subdomain.
I want users who type
http://www.example.com/word-of-the-day
to be taken to
http://www.example.com/index.php?page=word-of-the-day
But I want
http://www.example.com/word-of-the-day
to be shown in the URL for the user.
What should I do in my .htaccess? I tried but the regular expression
and the syntax of RewriteRule is way too complicated for me to
figure out how to do it.
Any help will be appreciated.
Edit:
Also, how can I say this in htaccess -
if they type http://www.example.com/word-of-the-day, take them to http://www.example.com/index.php?page=word-of-the-day
or if they type http://www.example.com/something-else, take them to http://www.example.com/index.php?page=something-else
or else, just take them to the URL they typed.
The condition below checks that index.php is not being requested. If not apply the rule. This will work for any of the scenarios you listed above.
RewriteCond %{QUERY_STRING} ^!.*[index\.php].*$ [NC]
RewriteRule ^(.*)$ index.php?page=$1 [L]
In response to your comment about only wanting to do this for a few specific pages, it would look like this(as an alternative to Nils edit):
RewriteCond %{QUERY_STRING} ^!.*[index\.php].*$ [NC]
RewriteCond %{REQUEST_URI} ^word-of-the-day$ [OR]
RewriteCond %{REQUEST_URI} ^something-else$ [OR]
RewriteCond %{REQUEST_URI} ^even-something-else$
RewriteRule ^(.*)$ index.php?page=$1 [L]
Try this
RewriteEngine on
RewriteRule ^word-of-the-day$ index.php?page=word-of-the-day
Or more flexible
RewriteEngine on
RewriteRule ^(.*)$ index.php?page=$1
Not tested, yet it sould work.
To your edit:
Just define those specific URLs manually:
RewriteEngine on
RewriteRule ^word-of-the-day$ index.php?page=word-of-the-day
RewriteRule ^word-some-example$ index.php?page=some-example
RewriteRule ^some-other$ index.php?page=some-other