I have .htaccess file with content below (it's slightly modified example from vue-router documentation):
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
</IfModule>
I want to:
redirect from www. to non-www (this is in in the above code and works)
add a rule to remove trailing slashes (so, a server should redirect from http://example.com/ to http://example.com)
redirect URLs like http://example.com/? to http://example.com
I tried to remove trailing slashes by:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
But server falls into redirect loop (301 to 302, 302 to 301, and so on) and I don't have any idea how to fix this.
The website is based on Vue.js with prerendering, so if it has http://example.com/contact page, the server should render file from /contact/index.html directory.
Related
When I enter my example.com url I want to redirect to example.com/en/ but when I enter example.com/demo I want to redirect to example.com/us/demo. The word demo here is just variable. how can I do that ?
My .htaccess code:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /tr/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /tr/index.php [L]
</IfModule>
Try the following before your existing directives (ie. before the <IfModule> section):
# Redirect "/" to "/en/"
RewriteRule ^$ /en/ [R,L]
# Redirect "/demo" to "/us/demo"
RewriteCond %{REQUEST_URI} !^/(en|us)/
RewriteRule (.*) /us/$1 [R,L]
The first rule redirects example.com/ to example.comn/en/. The second rule redirects everything else to /us/<url>, providing the URL-path does not already start /en/ or /us/.
If you also need to make an exception for your static assets... eg. Should a request for /assets/images/img.png also be redirected? Then include an additional condition (as the 2nd condition) on the second rule:
# Redirect "/demo" to "/us/demo" (and exclude static assets)
RewriteCond %{REQUEST_URI} !^/(en|us)/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) /us/$1 [R,L]
Note that these are 302 (temporary) redirects.
I have a rewrite rule to force user to go from example.com to www.example.com. This is for SEO reasons so that I have no duplicated websites and content on my Google results.
# BEGIN Spark
AddDefaultCharset UTF-8
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP:Authorization} ^(.*)
RewriteRule .* - [e=HTTP_AUTHORIZATION:%1]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [QSA,L]
RewriteCond %{HTTP_HOST} ^example.com
RewriteRule (.*) https://www.example.com/$1 [R=301,L]
</IfModule>
<IfModule mod_deflate.c>
<FilesMatch "\.(html|php|txt|xml|js|css|ttf|otf|ico|json|svg|)$">
SetOutputFilter DEFLATE
</FilesMatch>
</IfModule>
# END Spark
My question. If now people post a link example.com/news they always will redirect to the frontpage like www.example.com. How can I manage that they still can use and post short URLs like example.com/news1 or example.com/news2 and will redirect to www.example.com/news1 or www.example.com/news2 respectively.
You've put the rule in the wrong place. It needs to go before the internal rewrite (to the front-controller: index.php). eg. at the top of the file, not at the end.
By placing it last it will only correctly redirect physical directories (which includes the homepage) and static resources (images, CSS, JS, etc.). All other "short URLs" that are routed through the front-controller will be redirected to index.php and you'll see the "frontpage".
And... to avoid a double redirect when requesting example.com with a URL that contains a trailing slash then include an absolute URL in the trailing slash removal rule and include your non-www to www rule immediately after that.
For example:
RewriteEngine On
# Remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ https://www.example.com/$1 [L,R=301]
# Redirect non-www to www
RewriteCond %{HTTP_HOST} ^example.com
RewriteRule (.*) https://www.example.com/$1 [R=301,L]
RewriteCond %{HTTP:Authorization} ^(.*)
RewriteRule .* - [e=HTTP_AUTHORIZATION:%1]
# Front-controller
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]
You will need to clear the browser cache before testing since the erroneous 301 (permanent) redirects will have been cached by the browser. (Test with 302 - temporary - redirects to avoid caching issues.)
Aside: An additional concern is that these directives are inside a # BEGIN Spark ... # END Spark code block - which makes it look as if these directives are perhaps maintained by some automated process? In which case, they might be overwritten?
On my website, I only use 3 slashes in my URL path:
https://example.com/this/isatest/
Right now I use .htaccess which makes it possible (as a side effect) to add as many stuff on the URL as you like:
https://example.com/this/isatest/hipperdihopperdus/pizza/bacon/with/cheese
I'd like to automatically remove everything after "isatest" while keeping the trailing slash using .htaccess.
This is what my .htaccess currently looks like:
Options -Indexes
Options +FollowSymLinks
RewriteEngine on
# 301 Redirect all requests that don't contain a dot or trailing slash to
# include a trailing slash
RewriteCond %{REQUEST_URI} !/$
RewriteCond %{REQUEST_URI} !\.
RewriteRule ^(.*) %{REQUEST_URI}/ [R=301,L]
RewriteCond %{THE_REQUEST} /index\.html [NC]
RewriteRule ^index\.html$ /? [R=301,L,NC]
RewriteRule ^listen/$ /console/ [NC,L]
# Rewrites urls in the form of /parent/child/
# but only rewrites if the requested URL is not a file or directory
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?page=$1 [L,QSA]
How can I achieve this?
As your first rule, after the RewriteEngine directive, you can do something like the following:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+/[^/]+/). /$1 [R=302,L]
This checks if there is anything else (the dot) after two path segments and a slash, and redirects to removed "anything else".
Note that this is a 302 (temporary) redirect. Only change this to a 301 (permanent) redirect - if that is the intention - once you have confirmed that it works OK. This is to avoid the browser caching erroneous redirects whilst testing.
UPDATE: It may be more efficient to simply avoid redirecting files that end in a recognised file extension. Or perhaps exclude known directory location(s) of your static resources. For example:
RewriteCond %{REQUEST_URI} !\.(css|js|jpg|png|gif)$ [NC]
RewriteRule ^([^/]+/[^/]+/). /$1 [R=302,L]
OR,
RewriteCond %{REQUEST_URI} !^/static-resources/
RewriteRule ^([^/]+/[^/]+/). /$1 [R=302,L]
You can add this rule just below RewriteEngine On line:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+/[^/]+/).+$ /$1 [R=301,L,NE]
Just launched a new site. Kept the old site, but assigned it to a subdomain.
I need to forward requests for old site URLs to the new subdomain.
Old site URLs all have this structure: "www.oldsite.com/BMT/xxxxxx" where the "/BMT/" directly follows the .com.
I need my .htaccess to forward URLs like that to "subdomain.oldsite.com" instead of "www.oldsite.com", keeping the same path after the "/BMT/"
Any URLs without the "/BMT/" should not be redirected.
From my research, I think this might work
RewriteCond %{REQUEST_URI} ^/BMT http://subdomain.oldsite.com%{REQUEST_URI} [L,R=301]
Am I on the right track?
If so, where should I add it in my current .htaccess?
Current .htaccess is:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Yes, you are right,
RewriteCond %{REQUEST_URI} ^/BMT [NC]
is a condition ,if it matchs the request uri string starting with /BMT then the rule will redirect the uri to http://sub.domain .
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_URI} ^/BMT [NC]
RewriteRule ^(.*)$ http://sub.oldsite.com/$1 [NC,R,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
I want to make a particular folder to be treated as an actual one, not as a controller in codeigniter and also want to redirect my all domain.com requests to www.domain.com. I have the following htaccess code:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond $1 !^(FOLDER_TO_BE_EXCLUDED)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Redirect non-www to www:
#RewriteCond %{HTTP_HOST} !^www\. [NC]
#RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
# Rewrite all other URLs to index.php/URL
RewriteRule ^(.*)$ index.php?url=$1 [PT,L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 index.php
</IfModule>
If I uncomment the lines (Redirect non-www to www), all requests are treated as controller
so the folder (FOLDER_TO_BE_EXCLUDED) is also treated as controller. I want to access this folder as an actual one.
If I will not redirect non-www to www I cant access session variables on inner pages.
Hope you will understand.
RewriteCond only apply to the RewriteRule which follows: your rules (commented now) were incorrect.
Besides, IMO, the simplest way to define exceptions like this one, is to use a non-rewriting rule like: RewriteRule ^FOLDER_TO_BE_EXCLUDED/ - [L] (on "top" of rewrite rules)
RewriteEngine On
# Redirect non-www to www:
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* http://www.%{HTTP_HOST}/$0 [R=permanent,L]
RewriteRule ^FOLDER_TO_BE_EXCLUDED/ - [L]
# Rewrite all other URLs to index.php/URL
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php?url=$0 [PT,L]
Try adding a RewriteCond to your last RewriteRule:
RewriteCond $1 !^(folder_name/)
RewriteRule ^(.*)$ index.php?url=$1 [PT,L]
This should stop it redirecting www.yourdomain.com/folder_name to the index.php.
You can add more folders to the condition using the | character:
RewriteCond $1 !^(folder_name/|another_folder/)