.htaccess Redirect 301 with ? (without variables) - regex

There are some urls with ?.
/shop/index.php?route=affiliate/register
/shop/?route=affiliate/register
It is needed for these urls to be redirected to:
/another-page/
(asume all urls begin with the home directory. example: www.homepage.com/shop?route=affiliate/register )
I have tried both simple apache Redirect 301 and rewrite.

You may use this rule as your top rule in site root .htaccess:
RewriteEngine On
RewriteCond %{THE_REQUEST} /shop/(?:index\.php)?\?route=[^\s&]+ [NC]
RewriteRule ^ /another-page/? [R=301,L,NE]

Related

Redirect non-www to www giving me hundreds of 404's

Thanks to anyone who can take a moment to look at this.
Recently I created a new section "subdomain" in my website and in this new folder I have includes a Joomla CMS installation the url looks like this: http://www.example.com/subdomain/
In this folder I have a htaccess file to which I have added.
## No directory listings
# Redirect non-www to www:
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
When I try to access say http://example.com/subdomain/anytrailingstring then it's NOT redirecting me to http://www.example.com/subdomain/anytrailingstring as I expected, it is redirecting to http://www.example.com/anytrailingstring leaving out the /subdomain/ and this is of course a page that doesnt exist and therefore a 404.
This is a problem.
I do not have any directive in the root .htacces file except for this :
DirectoryIndex index.php
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Can someone perhaps see why the subdomain htaccess isnt redirecting to correctly? Did I miss something?
I am not good with htaccess at all, if anybody can help me I would really appreciate it.
Thanks!
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
You need to use the REQUEST_URI server variable instead of the backreference ($1). The URL-path matched by the RewriteRule pattern (first argument) is relative to the current directory, so excludes the parent subdirectory (ie. /subdomain in your example).
Do it like this instead:
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
You will need to clear your browser cache since the erroneous (301 - permanent) redirect will have been cached by the browser. Test with 302 (temporary) redirects to avoid potential caching issues.
However, a couple of questions:
Why are you not using HTTPS? (You are redirecting to HTTPS in the parent .htaccess file - but this is now being overridden by the mod_rewrite directives in the subdirectory.)
Why not include this in the parent .htaccess file?
UPDATE: So, taking the above points into consideration... if you want to move this rule to the parent .htaccess file in the root then have it like this:
DirectoryIndex index.php
Options +FollowSymLinks
RewriteEngine on
# Redirect non-www to www (and HTTPS)
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# Redirect HTTP to HTTPS
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
The order of the directives is to ensure there is only ever at most 1 redirect (assuming you are not implementing HSTS).
You were unnecessarily duplicating the RewriteEngine directive (so I removed the second instance).
The RewriteBase directive was not being used.
The capturing subgroup in your HTTP to HTTPS rule was not required. ie. ^ is better than ^(.*)$ in this instance.
Aside:.
...a new section "subdomain" in my website and in this new folder I have includes a Joomla CMS installation the url looks like this: http://www.example.com/subdomain/
This is a subdirectory, not a "subdomain".
This is a "subdomain":
http://subdomain.example.com/

Add Virtual Folder to all urls (Apache)

Using Apache and .htaccess how can I do the following:
Redirect all urls to include a fake folder.
For example:
http://example.com/
http://example.com/example.php
http://example.com/folder/example.php
redirect to:
http://example.com/fakefolder/
http://example.com/fakefolder/example.php
http://example.com/fakefolder/folder/example.php
fakefolder does not actually exist in the server directories so I also need to point http://example.com/fakefolder/ to http://example.com/.
So basically if the user goes to http://example.com/ I want the url to show http://example.com/fakefolder/ but at the same time still point to http://example.com.
You can use in your htaccess:
RewriteEngine on
RewriteCond %{THE_REQUEST} !\s/+fakefolder/ [NC]
RewriteRule ^ /fakefolder%{REQUEST_URI} [R=301,NE,L]
RewriteRule ^fakefolder(/.*)$ $1 [NC,L]

Redirect from page without parameters to page with parameters in htaccess

I need to redirect from page like this www.site.ru/machines/ to page www.site.ru/machines/?set_filter=y&PRICE_MIN=4500 in the mod_rewrite section of .htaccess.
You can use this rule in root .htaccess:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^(machines/)$ /$1?set_filter=y&PRICE_MIN=4500 [L,NC,R]

301 Redirect Dynamic URL with .htaccess

I have dynamic URL that I want to redirect (301) with .htaccess.
Old URL:
mysite.com/index.php?route=product/search&keyword=searchphrase
New URL:
mysite.com/index.php?route=product/search&search=searchphrase
I need that the redirect will change the URL from search&keyword to search&search, but will keep the searchphrase.
How to do this?
Thanks!
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(index\.php\?route=product/search)&keyword=([^\s&]+) [NC]
RewriteRule ^ /%1&search=%2 [R=301,L,NE]

Magento htaccess Rewrite and Redirect

On a Magento based ecommerce site I am trying to remove index.php from URLs as well as 301 redirect non-www to www.
Default URL: www.example.com/index.php/super-cool-product.html
Desired Product URL: www.example.com/super-cool-product.html
Also 301 redirecting non-www to www:
example.com/super-cool-product.html
to:
www.example.com/super-cool-product.html
As well as:
www.example.com/index.html
to:
www.example.com
This is what I currently have:
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301]
RewriteCond %{THE_REQUEST} ^.*/index.php
RewriteRule ^(.*)index.php$ http://www.rejuvahealth.com/$1 [R=301,NC,L]
RewriteCond %{THE_REQUEST} ^.*/index.html
RewriteRule ^(.*)index.html$ http://www.rejuvahealth.com/$1 [R=301,NC,L]
To 301 redirect index.html:
Options +FollowSymLinks
RewriteEngine on
redirect 301 /index.html http://www.example.co.uk/
If you configure your Base URLs correctly, Magento should automatically redirect to the www. version.
For index.php rewrites, go to Configuration > Web > Use Web Server Rewrites and change to 'Yes'.
Now open your htaccess and change this line:
#RewriteBase /magento/
to this:
RewriteBase /
Assuming that your Magento folder is on the root. Be careful when using web rewrites though and ensure you back up before making any changes.