.htaccess mod_rewrite redirect without query string variables not working - apache

I'm having a problem with one of my rewrite rules. I would like to redirect all of the following URL's to another URL without the query string.
/gallery/products.aspx?C=9&SC=&ID=428&P=10
/gallery/products.aspx?C=2&SC=2&ID=128&P=1
/gallery/products.aspx?ID=147&C=2&SC=&P=7
/gallery/products.aspx?ID=1337&C=15&SC=&P=1
/gallery/products.aspx?ID=1532&C=3&SC=&P=2
/gallery/products.aspx?C=9&SC=&ID=1489&P=1
/gallery/products.aspx?C=7&SC=&ID=100&P=2
/gallery/products.aspx?C=2
/gallery/products.aspx?ID=1328&C=14&SC=11&P=17
/gallery/products.aspx?C=1&SC=&ID=767&P=3
/gallery/products.aspx?ID=1270&C=1&SC=&P=26
and I have this in my .htaccess file
RewriteRule ^gallery/products.aspx http://www.domain.com/category/? [L,R=301]
but it's not working. I checked it in a .htaccess simulator and it found the rule then redirected, but when I upload to my server, it doesn't redirect. I've also tried some other rules with no luck

I was finally able to make this work with the following:
RewriteCond %{HTTP_HOST} www.domain.com [NC]
RewriteRule products.aspx http://www.domain.com/category? [L,R=301]

Related

Htaccess Redirect URL with two forward slashes (not double) won't work

I want to redirect from one domain to a new domain. At the same time, the URL structure has changed.
Old: https://www.olddomain.com/parentpage/oldtitle/
New: https://www.newdomain.com/newtitle
This is wordpress, and I placed this code above the Wordpress stuff, as well as tested it here: https://htaccess.madewithlove.be/
I tried this, which doesn't work:
Redirect 301 /parentpage/title https://www.newdomain.com/newtitle
Also, when testing it at https://htaccess.madewithlove.be/, I do have this redirect:
Redirect 301 /parentpage https://www.newdomain.com/parentpage
The tester would skip my preferred redirect above, and use this one, leaving me with this, which does not exist:
https://www.newdomain.com/parentpage/oldtitle
Even when I place the preferred redirect above this one. I need both, unfortunately.
Have also tried the following RewriteRules (not all at the same time)
ReWriteRule https://www.olddomain.com/parentpage/oldtitle/ https://www.newdomain.com/newtitle
ReWriteRule /parentpage/oldtitle/ https://www.newdomain.com/newtitle
ReWriteRule "https://www.olddomain.com/parentpage/oldtitle/" "https://www.newdomain.com/newtitle"
I think it has something to do with that second forward slash separating the parentpage name and page title, but I can't figure out how to fix it.
In RewriteRule it wouldn't match http or https in it, you may try following.
please make sure you clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteCond %{HTTP_HOST} ^(?:www\.)olddomain\.com [NC]
RewriteCond %{REQUEST_URI} ^/parentage/oldtitle/?$ [NC]
RewriteRule ^(.*)$ https://www.newdomain.com/newtitle [R=301,L]

Apache mod_rewrite from a folder to a different domain doesn't work with trailing slash

I'm trying to rewrite URLs from old.domain.tld/project to domain.tld/subfolder/project using .htaccess in the project directory on the old server. I think I need to check for the host name so the rule only applies to the old server, even if the .htaccess file is also put on the new server.
I've tried the following .htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^old\.domain\.tld$ [NC]
RewriteRule ^(.*)$ https://domain.tld/subfolder/project/$1 [R=301,L]
This works fine for URLs like https://old.domain.tld/project/index.php?p=q (redirects to https://domain.tld/subfolder/project/index.php?p=q) but not for the URL without the trailing slash—https://old.domain.tld/project ends up being redirected to https://domain.tld. Very odd! How do I make this work for both types of URL?
I tried a rather different method to make this a generic redirect, that could be included even if the folder name wasn't project, but this has the same problem:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^old\.domain\.tld$ [NC]
RewriteCond %{REQUEST_URI} ^(.*)$
RewriteRule . https://domain.tld/subfolder/%1 [R=301,L]
In a server-wide configuration, a slash is appended to all requests for index files, e.g. old.domain.tld/project is redirected to old.domain.tld/project/. Could this be causing a problem?

having trouble writing htaccess redirects

I'm having trouble writing redirect rules in my website's htaccess file.
Basically, i want to write two rules:
1 - When i write the base URL, like http://www.example.com, i want it to automatically redirect the user to http://www.example.com/someDirectory.
2 - However, when i write http://www.example.com/Admin, i want it to redirect the user to http://www.example.com/Admin.
Here's what i've managed to do so far:
# This allows you to redirect index.html to a specific subfolder
Redirect http://www.example.pt http://www.example.pt/MainFolder
# This allows you to redirect index.html to a specific subfolder
Redirect http://www.example.pt/Admin http://www.example.pt/Admin
However this does not work... Any idea on how to do this?
Try it like this,
When there is no request for specific file or directory it will redirect you to your directory mention in rule and for the rest it will work without any rule.
Please check.
RewriteEngine on
RewriteCond %{REQUEST_URI} ^$
RewriteRule ^ %{HTTP_HOST}/someDirectory [R,L]
After a long research i was able to find a solution to my problem. I'll leave it here, in case anyone's having the same problem:
#Rewrite everything to subfolder
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/MainFolder
RewriteCond %{REQUEST_URI} !^/Admin
Rewriterule ^(.*)$ MainFolder/$1 [L]

.htaccess redirect from directory to subdomain without changing url

I would like to redirect /blog/ to another project. Without changing the url.
The hole thing almost works with the redirect rules below and using Apache mod_proxy, but only if it hits the index itself on /blog/ it DOES redirect to blog.website.nl.
RewriteCond %{HTTP_HOST} ^website\.nl [NC]
RewriteRule ^(.*)$ http://blog.website.nl/$1 [P,L]
When I visit website.nl/blog/category/test/
it works fine!
So why is this still changing the url on /blog/ and how to fix that?
Thanks! This has been quite a puzzle for me so far.
Update:
I managed to get it working. Instead of adding a .htaccess file to blog folder I added this code to the root /htaccess file. Now it all works!
RewriteCond %{HTTP_HOST} ^website\.nl[NC]
RewriteRule ^/?blog/(.*)$ http://blog.website.nl/$1 [P,L]

apache .htaccess file https:// redirect ---- rewrite it for subdirectory?

here is my code:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
This will successfully rewrite everything to https (it will "force" https), if I put it in my home directory's .htaccess file. However, what about if I only want my subdirectory of /support to force https? How to rewrite the above code?
The intent is with regards to forcing https in WHMCS
Thanks in advance!
If you want to redirect only a subfolder and not a subdomain, that is even easier. To redirect the subfolder /support/ you would use the following code:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^/support/(.*)$ https://%{HTTP_HOST}/support/$1 [QSA]
I added a QSA flag, which will force any query string that was entered to be appended to the redirect URL. If you don't use query strings in this section of the site, you can remove that flag. Also, if the string as shown doesn't work, you can try removing the leading slash from the RewriteRule and retrying it.
Add the following rule before the %{HTTPS} line:
RewriteCond %{HTTP_HOST} ^support.domain.com$
This will only allow the rule to execute if the visitor is coming from the subdomain. Please note, these rules will likely need to be put in the .htaccess file that is the root for the subdomain, not the main site root folder.