So i have to change a url in .htaccess for SEO purposes, i already have a .htaccess file on the home directory of the site...
do i need to make the changes on the .htaccess on the home directory or on the subdirectory that is the web page that i need to have a new URL???
This is the code i already have on the home directory
RewriteEngine On
RewriteCond %{HTTPS} off
# First rewrite to HTTPS:
# Don't put www. here. If it is already there it will be included, if not
# the subsequent rule will catch it.
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Now, rewrite any request to the wrong domain to use www.
# [NC] is a case-insensitive match
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Redirect /public_html/cursos/YoutuberKids/index.php /public_html/cursos-
kids/Youtuber/index.php
and this is the actual URL of the page that must be changed:
https://www.viahavok.com.br/cursos/GameDesignPRO/
And i need to become this URL:
https://www.viahavok.com.br/cursos/game-design-pro
Why don't you just change the name of the folder to 'game-design-pro'? Changing the URL in .htaccess isn't better or more effective when it comes to SEO in this case. (Unless I misunderstood your question?)
Add this to the bottom of the .htaccess file in the home directory:
RewriteRule ^cursos/GameDesignPRO/(.*)$ /cursos/game-design-pro/$1 [R=301,NC,L]
This will redirect all resources in the directory.
The R=301 indicates that this is a permanent move for the
resources.
The [L] flag causes mod_rewrite to stop processing the rule set.
In most contexts, this means that if the rule matches, no further
rules will be processed.
The [NC] flag causes the RewriteRule to be matched in a
case-insensitive manner.
Related
On our shared hosting server, we had a request to keep client's domain on their server but redirect the DNS responsible for web traffic to our server.
On our server we had to add an entry in the .htaccess file in our root to point it to a folder in the server:
RewriteCond %{HTTP_HOST} ^(www\.)?example\.pl$ [NC]
RewriteCond %{REQUEST_FILENAME} !/WebsitesLive/Example/
RewriteRule ^(.*)$ /WebsitesLive/Example/$1 [L]
And the website works fine but we noticed in Google Analytics that some people access the website using https://example.pl/WebsitesLive/Example. I finally realised that (maybe) it's the HTTPS and non-www redirection in the htaccess file of the client's site:
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
Is it true that %{REQUEST_URI} would, in this case, contain WebsitesLive/Example in the redirection URL?
Most importantly, how do I stop it?
Is it true that %{REQUEST_URI} would, in this case, contain WebsitesLive/Example in the redirection URL?
Yes, after the internal rewrite from the root the REQUEST_URI server variable is updated to include the full URL-path. You could instead capture the URL-path in the RewriteRule pattern and use the backreference which will be relative to the directory that contains the .htaccess file.
For example:
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}/$1 [R=301,L]
(You were already capturing the URL-path, but not using it.)
Ordinarily, if this canonical redirect was used in a subdirectory and that subdirectory was part of the visible URL then this would be incorrect, since it would remove the subdirectory from the redirected URL.
Alternatively, you could implement the canonical redirects in the root .htaccess file instead.
However, whilst this should prevent the filesystem directory being exposed in the canonical redirect, this doesn't prevent a user from accessing this subdirectory (from any domain). And since this subdirectory has already been exposed (especially as a 301 permanent redirect) then direct access to this subdirectory needs to be blocked or redirected back to the root. However, we need to be careful of redirect loops when doing so.
You can use something like the following in the subdirectory .htaccess to redirect any "direct" requests from the user back to the root:
# /WebsitesLive/Example/.htaccess
# Redirect direct requests to subdirectory back to root
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule (.*) https://%{HTTP_HOST}/$1 [R=301,L]
The REDIRECT_STATUS environment variable is empty on direct requests, but set to the HTTP status code after the first internal rewrite - thus preventing internal rewrites to the subdirectory being redirected back to the root (an endless redirect loop).
Aside:
RewriteCond %{HTTP_HOST} ^(www\.)?example\.pl$ [NC]
RewriteCond %{REQUEST_FILENAME} !/WebsitesLive/Example/
RewriteRule ^(.*)$ /WebsitesLive/Example/$1 [L]
Whilst this might "work", the second condition is too broad since it is checking that /WebsitesLive/Example/ does not occur anywhere in the "filesystem path". Whereas you should be checking that /WebsitesLive/Example/ does not occur at the "start" of the URL-path. In other words, it should be like this:
:
RewriteCond %{REQUEST_URI} !^/WebsitesLive/Example/
:
Note that this condition is only necessary (to prevent a rewrite loop) if there is no .htaccess file in the subdirectory (being rewritten to) that contains mod_rewrite directives. (Since mod_rewrite directives in the subdirectory completely override the parent - by default.)
If there is no .htaccess file in the subdirectory then you would obviously need to prevent direct access to that subdirectory in the root .htaccess file, but the required rule would be slightly different to the above. For example:
# /.htaccess
# Redirect direct requests to subdirectory back to root
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^WebsitesLive/Example/(.*) https://%{HTTP_HOST}/$1 [R=301,L]
# Rewrite to subdirectory
:
I need to rewrite the URL from https://example.com/dir/files to https://dir.example.com/files. When the user finishes registration on my website, the system creates a new directory for that user. That directory name depends on what the user enters as a username, so that means if the user enters moderator as username, the directory will get the name moderator so his URL need's to be https://moderator.example.com/files. I try with this code, but not working for me:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^dir/install$ https://dir.example.com/install? [R=301,L]
My complete .htacess file:
Options +MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteEngine on
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^dir/install$ https://dir.example.com/install? [R=301,L]
disable directory browsing
Options All -Indexes
I don't have experience with .htaccess so can someone explain to me how this works and where I make mistakes. Thanks all.
There are a few issues with the directives as posted.
MultiViews needs to be disabled. This is conflicting with the first rule that appends the .php extension via an internal rewrite.
The directives are in the wrong order. The redirect to the subdomain needs to be before the rule that appends the .php extension.
The rule that appends .php isn't strictly correct, since .php is appended to "any" URL that does not exist, even though the target .php file might also not exist.
I've made a few additional assumptions in order to determine the URL that should be redirected to the subdomain:
The URL consists of exactly two path segments, ie. /<subdomain>/<file> (no trailing slash).
The <file> (and <subdomain>) cannot contain dots.
/<subdomain>/<file> cannot map to a physical directory.
Try the following instead:
# Disable directory browsing and MultiViews
Options All -Indexes -MultiViews
RewriteEngine On
# Redirect to subdomain
RewriteCond %{HTTP_HOST} ^(example\.com) [NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/.]+)/([^/.]+)$ https://$1.%1/$2 [R=302,L]
# Internal rewrite to append ".php" extension
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^([^.]+)$ $1.php [L]
The %1 backreference in the first rule contains the domain name captured from the preceding condition. Unlike the $n backreferences that are captured from the RewriteRule pattern.
On page when i open it as https://dir.example.com/install i get admin.example.com’s DNS address could not be found. Diagnosing the problem.
You need to make sure the DNS is correctly configured (presumably with a "wildcard" subdomain) and the server is configured to receive such requests. This is not covered in the above rules.
I having a problem with redirection of my website. A few days ago I bought a .com domain. There are two languages: Italian and German. German website was set on example.de and English version on example.it.
Now I changed it to .com domain: example.com with subfolders: example.com/it/ and example.com/it/.
How to redirect all links from example.it to example.com/it/ and example.de to example.com/de/.
...and following subfolders.
I was trying with
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^example\.de$ [NC]
RewriteRule ^(.*)$ https://example.com/de/ [R=301,L]
But without success.
RewriteCond %{HTTP_HOST} ^example\.de$ [NC]
RewriteRule ^(.*)$ https://example.com/de/ [R=301,L]
Having captured the URL-path in the RewriteRule pattern, you are missing the corresponding backreference in the substitution string, so this will redirect everything to the homepage, ie. example.de/<url> to example.com/de/ - the <url> is lost.
However, you can handle both TLDs with a single rule. Since the TLD is the same as the subdirectory.
I'm assuming all three domains resolve to the same place.
Try the following instead in the root .htaccess file:
RewriteCond %{HTTP_HOST} ^example\.(de|it) [NC]
RewriteRule (.*) https://example.com/%1/$1 [R=301,L]
The %1 backreference refers to the captured TLD from the preceding CondPattern. And the $1 backreference contains the URL-path captured in the RewriteRule pattern.
The RewriteBase directive is not required here.
Test with a 302 (temporary) redirect to avoid caching issues. You will need to clear your browser cache before testing, since the erroneous 301 (permanent) redirect will have been cached.
UPDATE#1: This is Polylang plugin for Wordpress.
In which case, this redirect directive must go at the very top of your .htaccess file, before any existing WordPress directives, including before the # BEGIN WordPress comment marker.
UPDATE#2: I also have .cz domain and subfolder is /cs (ISO standard)
Since the TLD and subdirectory are different, you will need to add another rule to handle this. (This can go before or after the above rule, since there is no conflict.)
For example:
RewriteCond %{HTTP_HOST} ^example\.cz [NC]
RewriteRule (.*) https://example.com/cs/$1 [R=301,L]
Following are the redirection rule I have in my htaccess file. They redirect https://olddomain.com to https://subdomain.domain.com
but the web pages are not getting redirected. I still have olddomain.com/page1 loading.
`RewriteCond %{HTTP_HOST} ^old\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.old\.com$
RewriteRule ^/?$ "https\:\/\/subdomain\.domain\.com\/" [R=301,L]`
I added the following rule which is working partially, the slash after the domain is missing. Now the redirect is https://subdomain.domain.compage1 instead of
https://subdomain.domain.com/page1
RewriteRule ^ https\:\/\/subdomain\.domain\.com\/{REQUEST_URI} [L,R=301]
How to fix this. Any help please. (I tried the redirect without escaping \ at the end but this didn't work.
RewriteRule ^ https\:\/\/subdomain\.domain\.com{REQUEST_URI} [L,R=301])
RewriteCond %{HTTP_HOST} ^old\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.old\.com$
RewriteRule ^/?$ "https\:\/\/subdomain\.domain\.com\/" [R=301,L]
These redirect the homepage (root directory) only. To redirect any URL-path to the same URL-path at the target use the following instead:
RewriteCond %{HTTP_HOST} ^(www\.)?old\.com [NC]
RewriteRule ^ https://subdomain.domain.com%{REQUEST_URI} [R=301,L]
I've combined your two conditions.
Note that the syntax is %{REQUEST_URI} (with a % prefix) to reference the REQUEST_URI server variable. "{REQUEST_URI}" is otherwise just literal text.
There is no need to backslash-escape all those characters in the substition string.
Clear your browser cache before testing. And preferably test with 302 (temporary) redirects to avoid potential caching issues.
I added the following rule which is working partially, the slash after
the domain is missing. Now the redirect is
https://subdomain.domain.compage1 instead of
https://subdomain.domain.com/page1
RewriteRule ^ https\:\/\/subdomain\.domain\.com\/{REQUEST_URI} [L,R=301]
Except that that is not possible with the directive as stated - you were perhaps seeing a cached response as a result of earlier erroneous redirects. 301 (permanent) redirects are cache persistently by the browser. Always test with 302 (temporary) redirects and ideally with the browser cache disabled (option on the "Network" tab) of the browser dev tools.
Alternative solution
This would need to go in the root .htaccess file (it would not work if it is in a subdirectory .htaccess, unlike the rule above).
RewriteCond %{HTTP_HOST} ^(www\.)?old\.com [NC]
RewriteRule (.*) https://subdomain.domain.com/$1 [R=301,L]
This uses a backreference instead of the REQUEST_URI server variable.
This redirect needs to go near the top of the .htaccess file, before most other directives.
Make sure you've cleared your browser cache before testing.
This is my current .htaccess file:
RewriteEngine on
RewriteCond %{REQUEST_URI} !=/khp/handler.php
RewriteRule (.*) /khp/handler.php?url=$1 [L,QSA]
The desired result is for all urls, no matter whether or not they exist on the server, to be rewritten internally to /khp/handler.php?url=$1, where $1 is the original url.
What is currently happening is when I go to a url that is the name of an existing folder, it does this:
Requested url: example.com/khp
Redirects to: example.com/khp/?url=
Ideally, it would also redirect to remove all trailing slashes (ALL, whether an existing directory, the bare domain name example.com, etc). This was in a previous iteration of my htaccess file, but I removed it because it was creating an infinite redirect loop on example.com/khp
What am I doing wrong?
That happens because /khp is a real directory and mod_dir runs after mod_rewrite and adds a trailing slash in the rewritten URI.
To turn this behavior off use:
DirectorySlash Off
RewriteEngine On
RewriteBase /
# everything
RewriteCond %{REQUEST_URI} !=/khp/handler.php
RewriteRule (.*) /khp/handler.php?url=$1 [L,QSA]
# add a trailing slash to directories via a rule
RewriteCond %{DOCUMENT_ROOT}/$1 -d
RewriteRule ^(.*?[^/])$ %{REQUEST_URI}/ [L,R=302]