I want to make a redirection using htaccess but I run into an issue with trailing slash.
I want to redirect from https://test.com/transfer or https://test.com/transfer/
to https://test2.com/destination
Below is my htaccess rule but it seems to be only working for https://test.com/transfer but NOT https://test.com/transfer/
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/transfer$ [NC]
RewriteRule ^ https://somesite.com/destination/$1 [R,L]
What is wrong with my rule?
Try:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/transfer/?$ [NC]
RewriteRule ^ https://somesite.com/destination/$1 [R,L]
or
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/transfer/?$ https://somesite.com/destination/ [NC,R,L]
Make sure you use RewriteEngine On only once.
If you want to redirect everything inside transfer: if transfer is a dir btw, use:
RewriteEngine on
RewriteRule ^/transfer/?(.+)?$ https://somesite.com/destination/$1 [R, L]
Related
I have done a 301 redirect for all trailing splash URLs in my htaccess file. Now I have a few dynamic URLs `website.com/item/activate/#dynamiclychangingvalue**/** that need the splash. Note that in the end on the activate folder is a dynamic url that needs the splash.
How would I edit the .htaccess file? I have tried all sorts of codes, they dont seem to be working.
IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [R=301,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
the dynamic url looks like this website.com/item/activate/#dynamiclychangingvalue**/**
You can use:
RewriteCond %{REQUEST_URI} !^/additem/ [NC]
RewriteRule ^(.+)/$ $1 [R=301,L]
If I open a non existing url on my site like www.domain.de/abcde/ (with trailing slash) the url redirects to www.domain.de/abcde (without trailing slash) and opens the 404 site after.
But the 404 site should come directly when the url does not exist (without redirecting for the non-trailing-slash policy).
What do I need to add to my htaccess? Thank you!
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [R=301,L]
------
starkeens suggestion does only work on a clean htaccess, but I got some more code. It does not work together with the other lines. All the code:
RewriteEngine on
RewriteBase /
# redirect urls without www. to url with www.
RewriteCond %{HTTP_HOST} ^domain\.de$
RewriteRule ^(.*)$ http://www.domain.de/$1 [L,R=301]
# hide the suffix of urls
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.+)\.html\ HTTP/
RewriteRule (.*).html$ /$1 [R=301,L]
# start redirect everything to the subfolder
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.de [NC]
# except these folders:
RewriteCond %{REQUEST_URI} !^/downloads [NC]
RewriteCond %{REQUEST_URI} !^/cms [NC]
RewriteCond %{REQUEST_URI} !^/vorschau [NC]
RewriteCond %{REQUEST_URI} !^/domain/.*$
RewriteRule ^(.*)$ /subfolder/$1
# end redirect everything to the subfolder
# no-trailing-slash policy
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [R=301,L]
Any new suggestions? :)
TRY :
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ - [R=404,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [R=301,L]
Finally i got a solution that seems to work.. maybe anyone got the same issue and needs the code. Comments for optimization are welcome!
# trailing Slashes
# to enforce a no-trailing-slash policy with subfolder
# is the request for a non-existent file?
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# if so, skip the next RewriteRule
RewriteRule .? - [S=1]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [R=301,L]
Now only existing files get the redirect of my no-trailing-slash policy. Non existing files aren't redirected and occur a 404 page directly, as wished.
I'm trying to get rid of some subdirectories on my website.
I tried this link Remove two subdirectories from url with mod_rewrite but it does not work with my case.
Actually i have this structure : www.mywebsite.com/fr/news/index.html and I want it to look like www.mywebsite.com/index.html.
In my root directory a have a htaccess file with the following:
Options -Indexes +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.html [L]
RewriteCond %{REQUEST_URI} !^/(en|es|pt)/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+fr/([^\s]+) [NC]
RewriteRule ^ %1 [R=301,L]
RewriteCond %{REQUEST_URI} !^/(en|es|pt)/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (?!^fr/)^(.*)/$ /fr/$1 [L,NC]
RewriteCond %{REQUEST_URI} !^/(en|es|pt)/
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*[^.]+\.html(\?[^\ ]*)?\ HTTP/
RewriteRule ^(([^/]+/)*[^.]+)\.html$ http://%{HTTP_HOST}/fr/$1 [R=301,L]
RewriteCond %{REQUEST_URI} !^/(en|es|pt)/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ http://%{HTTP_HOST}/fr/$1/ [R=301,L]
What would the change to do to make it work ?
Thank you in advance for your answers
I have this structure:
www.mywebsite.com/fr/news/index.html
and I want it to look like
www.mywebsite.com/index.html
Without knowing more, I'd suggest placing this in your root folder:
RewriteEngine on
RewriteRule ^[^\/]+\/[^\/]+\/(.*) http://www.mywebsite.com/$1
The regex reads as:
from the start, find one or more characters that aren't a forward slash
then a forward slash
then one or more characters that aren't a forward slash
then a forward slash
then capture all the remaining characters
redirection and mapping for same page using htaccess not working
Ex . www.xyz.com?view=aa¶m=bb -- redirect it to www.xyz.com/aa/bb
Then map www.xyz.com/aa/bb to
www.xyz.com?view=aa¶m=bb
below is the rules.
redirection
RewriteCond %{QUERY_STRING} ^view=([^&]*)&p=([0-9]+)&/(.*)$
RewriteRule ^insights/(.+?)\.html$ insights/$1/%1/%2-%3.html? [R=301,L]
mapping
RewriteRule ^insights/(.*)/(.*)/([0-9]+)-(.*)\.html$ /insights/$1.html?view=$2&p=$3&/$4 [L,QSA,NC]
This should work to what you're trying to do:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
# Redirect /insights/anything.html?view=anything&p=anything&/anything
# To /insights/anything/anything-anything.html
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(insights)/([^.]+)\.html\?view=([^&\s]+)&p=([^&\s]+)&/([^&\s]+) [NC]
RewriteRule ^ /%1/%2/%3/%4-%5.html? [R=302,L]
# Internally forward /insights/anything/anything-anything.html
# To /insights/anything.html?view=anything&p=anything&/anything
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^-]+)-([^.]+).html$ /$1/$2.html?view=$3&p=$4&/$5 [L]
Change R=302 to R=301 only after you have confirmed the rule is working to avoid your browser from caching wrong redirects.
Keep these 2 rules at top of your .htaccess:
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+\?view=([^&]+)¶m=([^\s&]+) [NC]
RewriteRule ^ /%1/%2? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/?$ /?view=$1¶m=$2 [L,QSA]
I am trying to add some code to my .htaccess to redirect slash and non slash urls to the .html all url's apart from my homepage.
For example
www.mydomain.com/cat/ and www.mydomain.com/cat
should redirect to www.mydomain.com/cat.html
I have managed to add the following to my .htaccess which redirects www.mydomain.com/cat to the right place www.mydomain.com/cat.html but need some help on how to make slash version redirect to the .html page
RewriteCond %{REQUEST_URI} !\.[^./]+$
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://mydomain.com/$1.html [R=301,L]
My whole .htaccess looks like this, if anyone has any suggestions on how it should look in light of the above it would be greatly appreciated.
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^xxx.xxx.xxx.xx [nc,or]
RewriteCond %{HTTP_HOST} ^mydomain.com [NC]
RewriteRule ^(.*)$ http://www.mydomain.com/$1 [L,R=301]
RewriteCond %{THE_REQUEST} ^.*/index.php
RewriteRule ^(.*)index.php$ /$1 [R=301,L]
RewriteCond %{REQUEST_URI} !\.[^./]+$
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://mydomain.com/$1.html [R=301,L]
DirectoryIndex index.html index.php
<IfModule mod_rewrite.c>
RewriteEngine on
# Pleas note that RewriteBase setting is obsolete use it only in case you experience some problems with SEO addon.
# Some hostings require RewriteBase to be uncommented
# Example:
# Your store url is http://www.yourcompany.com/store/cart
# So "RewriteBase" should be:
# RewriteBase /store/cart
# RewriteBase /
RewriteCond %{REQUEST_FILENAME} !\.(png|gif|ico|swf|jpe?g|js|css)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php?sef_rewrite=1 [L,QSA]
</IfModule>
SOLVED:
I just added:
RewriteCond %{REQUEST_URI} ^(.+)/$
RewriteRule ^(.+)/$ /$1 [R=301,L]
The separate rules seems to be working for you, but I think you can simplify it to one rule, with an optional slash. Your rule redirects the slash to no-slash, which then redirects again to the .html. With one rule, you'd only have one redirect.
This has the standard RewriteCond that check if it's not a file or a folder, so it doesn't keep redirecting .html if it's already one. Then, the \? in the ReweriteRule is an optional slash.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ http://mydomain.com/$1.html [R=301,L]
If this is all in your domain, you can omit it from the result:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ /$1.html [R=301,L]
Also, note this will catch and work with subfolders, whether or not you mean it to. e.g.,
www.mydomain.com/animals/cat/ will redirect to www.mydomain.com/animals/cat.html