.htaccess rewrite URI w/ replace part of it - .htaccess

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.

Related

301 redirect from URL with GET-parameters to the homepage

I have an old website with Joomla 1.5. It has some strange links with GET-parameters, like this:
http://www.primavista.ru/images/stories/catalog/?rand=1186511674
http://www.primavista.ru/images/stories/catalog/?rand=145388433
http://www.primavista.ru/images/stories/catalog/?rand=1553907057
http://www.primavista.ru/images/stories/catalog/?rand=1563973527
http://www.primavista.ru/images/stories/catalog/?rand=1981273478
http://www.primavista.ru/images/stories/catalog/?rand=2139631800
http://www.primavista.ru/images/stories/catalog/?rand=366928750
http://www.primavista.ru/images/stories/catalog/?rand=524689684
http://www.primavista.ru/images/stories/catalog/?rand=569077423
http://www.primavista.ru/images/stories/catalog/?rand=573405687
http://www.primavista.ru/images/stories/catalog/?rand=879649167
I want make redirect theses links to the homepage.
I tried some different instructions in .htaccess:
RewriteCond %{QUERY_STRING} ^/images/stories/catalog/?rand=([0-9]*)$
RewriteRule ^(.*)$ https://primavista.ru/? [R=301,L]
RewriteCond %{QUERY_STRING} ^/images/stories/catalog/?rand=(.*)$
RewriteRule ^(.*)$ https://primavista.ru/? [R=301,L]
RewriteCond %{QUERY_STRING} (^|&)(rand)=[^&]+ [NC]
RewriteRule ^images/stories/catalog(/.*)?$ https://primavista.ru/? [R=301,L,NC]
But no one not working. Maybe here someone can help me with this. Thanks
This probably is what you are looking for:
RewriteEngine on
RewriteCond %{QUERY_STRING} rand=\d+
RewriteRule ^/?images/stories/catalog/?$ / [R=301,L]
It is a good idea to start out with R=302 temporary redirections and to only change that to R=301 permanent redirections one you are satisfied with everything. That prevents nasty caching issues on the client side ...
UPDATE:
Your comment below indicates that you actually ask to remove the GET parameter in the redirected request, which you never mentioned before...
You can use the additional QSD flag for that:
RewriteEngine on
RewriteCond %{QUERY_STRING} rand=\d+
RewriteRule ^/?images/stories/catalog/?$ /? [R=301,QSD,L]

How can i redirect /?lang=en|ru to /?lang=en#googtrans(en|ru)?

How can I add the Google translate parameter #googtrans(en|de) or other language, so the translation happens automatically?
Basically, when the user goes to https://example.com/page/?lang=de they are redirected to https://example.com/page/?lang=en#googtrans(en|de)
I use this .htaccess rule, but it's not working:
RewriteCond %{QUERY_STRING} ^lang=([a-z]{2})$
RewriteRule ^/?lang=en#googtrans(en|[a-z]{2}) [R=301,L]
EDIT: Adding edited Rules here.
RewriteEngine ON
RewriteCond %{THE_REQUEST} \s/page/?\?lang=([a-z]{2})\s [NC]
RewriteRule ^ page/?lang=%1#googtrans(%1) [R=301,L,NE]
With your shown samples(this is considering that you are hitting URL like: https://example.com/page/?lang=de in browser), please try following .htaccess Rules file. Please make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteCond %{QUERY_STRING} ^lang=([a-z]{2})$ [NC]
RewriteRule ^page/?$ page/?lang=%1#googtrans(%1) [R=301,L,NE]

Redirect by .htaccess doesn't work

I want the URL www.michelenerna.nl to be redirected to www.michelenerna.nl/wycliffe.
The actual Joomla path is /wycliffe =
http://www.michelenerna.nl/index.php?option=com_content&view=article&id=97&Itemid=249
So, I thought: the following lines in .htaccess should be appropriate, but it doesn't work. www.michelenerna.nl still directs to www.michelenerna.nl
This is the code:
Options SymlinksIfOwnerMatch
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.michelenerna.nl [NC]
RewriteRule ^article(.*)$ http://www.michelenerna.nl/index.php?option=com_content&view=article&id=97&Itemid=249/$1 [R=301,L]
Why doesn't it work?
Try using the following instead:
RewriteCond %{HTTP_HOST} ^(www.)?michelenerna.nl$ [NC]
RewriteRule ^(/)?$ wycliffe/index.php [L]
You can also achieve this in the your server cPanel by going to the "Redirect" page and selecting the options that apply to you.
The following works for me:
RewriteCond %{HTTP_HOST} ^(www.)?michelenerna.nl$
RewriteRule ^(/)?$ wycliffe [L]

complex mod_rewrite. Any idea?

I want to :
- switch from http to https if http is used
- redirect the subdomain to index?o=subdomain except www
- redirection the subdirectory to index?u=user
Example :
http://www.mydomain.com will be redirected to https://www.mydomain.com
http://subdomain.mydomain.com will be redirected to https://www.mydomain.com/index?o=subdomain
https://subdomain.mydomain.com will be redirected to https://www.mydomain.com/index?o=subdomain
http://subdomain.mydomain.com/user will be redirected to https://www.mydomain.com/index?o=subdomain&u=user
https://subdomain.mydomain.com/user will be redirected to https://www.mydomain.com/index?o=subdomain&u=user
Is mod_Rewrite the best to do that ? Any idea ?
Thanks in advance
I don't have time to test it right now, but you can try this and see if it works. There may be some potential for some things to go wrong, so if you have trouble with it I'd be happy to work out any kinks later. Also, I think that I covered everything you wanted to do, but let me know if I left something out.
RewriteEngine On
# Force redirect to HTTPS
RewriteCond %{HTTPS} !=on
RewriteRule ^.*$ https://%{HTTP_HOST}/$0 [R=301,L]
Edit: I've updated the ruleset below. I thought about your question though, and aren't you going to have issues attempting to serve up your subdomains over TLS/SSL? That aside, one of the following should do what you want (without errors this time, I hope):
If you wanted internal redirection:
RewriteCond %{HTTP_HOST} !=mydomain.com
RewriteCond %{HTTP_HOST} !^www
RewriteCond %{REQUEST_URI} !^/index
RewriteCond %{HTTP_HOST}%{REQUEST_URI} ^([^\.]+)[^/]*/([^/]+)?
RewriteCond %1&u=%2 ^([^&]+)(&u=.+)?
RewriteRule ^.*$ /index?o=%1%2
If you wanted external redirection:
RewriteCond %{HTTP_HOST} !=mydomain.com
RewriteCond %{HTTP_HOST} !^www
RewriteCond %{REQUEST_URI} !^/index
RewriteCond %{HTTP_HOST}%{REQUEST_URI} ^([^\.]+)[^/]*/([^/]+)?
RewriteCond %1&u=%2 ^([^&]+)(&u=.+)?
RewriteRule ^.*$ https://www.mydomain.com/index?o=%1%2 [R=301,L]

Htaccess Redirect

I am looking to make www.purchase.example.com redirect to purchase.example.com, below is an example of what I am trying to do:
RewriteCond %{HTTP_HOST} ^www\.purchase\.
RewriteRule (.*) http://purchase.DOMAIN_NAME/$1 [L,R]
I need a variable that will replace DOMAIN_NAME with simply purchase.example.com.
Obviously I can hard code the purchase.example.com but I will need the code to work on multiple sites. Any suggestions?
For your knowledge:
I used a RewriteCond backreference:
RewriteCond %{HTTP_HOST} ^www\.purchase\.(.*)
RewriteRule (.*) http://purchase.%1/$1 [L,R]
I would not do this in code, I would do this on the web hosting account.
If you need it a little more generic redirect for every domain starting with www.:
RewriteCond %{HTTP_HOST} ^www\.(.+)
RewriteRule ^ http://%0%{REQUEST_URI} [L,R=301]

Resources