I have changed the website URL structure and I want to redirect my users
The problem
The new structure is very different, and more strict.
OLD
1 - https://example.com/serie-tv
2 - https://example.com/serie-tv/1845578-the-walking-dead
3 - https://example.com/serie-tv/1845578-The-Walking-Dead/seasons/1
4 - https://example.com/serie-tv/1845578-the-walking-dead/seasons/1/episodes/1
NEW
1 - https://example.com/browse?type=series
2 - https://example.com/titles/1845578
3 - https://example.com/titles/1845578/season/1
4 - https://example.com/titles/1845578/season/1/episode/1
A redirection from https://example.com/serie-tv/1845578-the-walking-dead/seasons/1
to
https://example.com/titles/1845578-the-walking-dead/season/1 will not work.
Need to redirect just to https://example.com/titles/1845578/season/1
For now I only managed to redirect everything under
https://example.com/serie-tv /.....
to
https://example.com/browse?type=series
with this code:
RewriteCond %{HTTP_HOST} ^example\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule ^serie\-tv\/?(.*) "https\:\/\/example\.com\/browse?type=series" [R=301,L]
A slight complication comes about because you have seasons and episodes (with an s) in the old URL structure and season and episode (no s) in the new URL structure, so you can't use a general solution that simply copies the URL-path.
RewriteCond %{HTTP_HOST} ^example\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com$
If you only have a single domain then these conditions are superfluous.
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
For now, we can ignore these as well. cPanel will automatically inject these as required when auto-renewing your SSL certs.
For now I only managed to redirect everything under ...
I assume you don't want to redirect "everything" since your preceding example URLs do not state this?
RewriteRule ^serie\-tv\/?(.*) "https\:\/\/example\.com\/browse?type=series" [R=301,L]
There is unnecessary backslash escaping here that affects readability. There is no need to escape literal hyphens (-) and slashes in the RewriteRule pattern. And in the substitution string (which is an "ordinary" string, not a regex), there is no need to escape the colon (:), slash (again) and dot. These "literal" characters carry no special meaning in the context they are used. (This is typical output having used cPanel's redirection feature - which will often put them in the wrong place as well.)
Try something like the following instead, near the top of your .htaccess file:
RewriteEngine On
# Redirect "/serie-tv"
RewriteRule ^serie-tv$ /browse?type=series [R=302,L]
# Redirect "/serie-tv/1845578-the-walking-dead"
RewriteRule ^serie-tv/(\d+)-[^/]+$ /titles/$1 [R=302,L]
# Redirect "/serie-tv/1845578-the-walking-dead/seasons/1"
RewriteRule ^serie-tv/(\d+)-[^/]+/seasons/(\d+)$ /titles/$1/season/$2 [R=302,L]
# Redirect "/serie-tv/1845578-the-walking-dead/seasons/1/episodes/1"
RewriteRule ^serie-tv/(\d+)-[^/]+/seasons/(\d+)/episodes/(\d+)$ /titles/$1/season/$2/episode/$3 [R=302,L]
\d is a shorthand character class for digits (the same as [0-9]) and \d+ matches 1 or more digits.
$1, $2 and $3 are backreferences to the captured groups in the RewriteRule pattern. It's more efficient to test what you can in the RewriteRule pattern instead of using a preceding condition that checks the REQUEST_URI server variable.
Note that these are currently 302 (temporary) redirects. Only change to 301 (permanent) when you have tested that they work OK - in order to avoid caching issues.
You will need to clear your browser cache before testing.
Aside: The old URL structure, that includes the title maybe better from an SEO / useability perspective?
I think this should do what you need.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} /serie-tv$ [NC]
RewriteRule ^(.*)$ browse?type=series [R=301,L]
RewriteCond %{REQUEST_URI} /serie-tv/([0-9]*)\-([^/]*)/?(.*)? [NC]
RewriteRule ^(.*)$ titles/%1/%3 [R=301,L]
Related
I have been at this all day long, tried dozens of variations but can't quite seem to get this rewrite to work.
RewriteCond %{THE_REQUEST} /pwreset\.php\ [NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} \s(.+?)/+[?\s]
RewriteRule ^(.+?)/$ https\:\/\/www\.example\.com\/support\/pwreset\.php [L]
The URL it returns is:
https://www.web-jive.com/support/pwreset.php/?key=cdc3b1aa842785f7345be501a30ddc83
What I need to be removed is the pwrest.php trailing slash before the question mark. Where am I going wrong on this?
The idea is to have the first URL below, redirect to the second:
https://example1.com/pwreset.php?key=cdc3b1aa842785f7345be501a30ddc83
https://example2.com/support/pwreset.php?key=cdc3b1aa842785f7345be501a30ddc83
EDIT
Per Mr. White's suggestion, I'm posting the whole .htaccess file.
RewriteEngine On
# Announcements
RewriteRule ^announcements/([0-9]+)/[a-z0-9_-]+\.html$ ./announcements.php?id=$1 [L,NC]
RewriteRule ^announcements$ ./announcements.php [L,NC]
# Downloads
RewriteRule ^downloads/([0-9]+)/([^/]*)$ ./downloads.php?action=displaycat&catid=$1 [L,NC]
RewriteRule ^downloads$ ./downloads.php [L,NC]
# Knowledgebase
RewriteRule ^knowledgebase/([0-9]+)/[a-z0-9_-]+\.html$ ./knowledgebase.php?action=displayarticle&id=$1 [L,NC]
RewriteRule ^knowledgebase/([0-9]+)/([^/]*)$ ./knowledgebase.php?action=displaycat&catid=$1 [L,NC]
RewriteRule ^knowledgebase$ ./knowledgebase.php [L,NC]
#Password reset
RewriteCond %{QUERY_STRING} ^key=[0-9a-f]{32}$
RewriteRule ^pwreset\.php$ https://www.web-jive.com/support%{REQUEST_URI} [R=302,L]
#Redirect to new support URL
RewriteCond %{HTTP_HOST} ^members\.web\-jive\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.members\.web\-jive\.com$
RewriteRule ^/?$ "https\:\/\/www\.web\-jive\.com\/support" [R=301,L]
RewriteCond %{HTTP_HOST} ^members\.web\-jive\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.members\.web\-jive\.com$
The output you are seeing (with the trailing slash on the URL-path) isn't the result of just the directives you posted, so maybe you have a conflict with other directives or you are seeing a cached response.
However, the rule you posted would seem to be far more complex than it needs to be.
1. https://example1.com/pwreset.php?key=cdc3b1aa842785f7345be501a30ddc83
2. https://example2.com/support/pwreset.php?key=cdc3b1aa842785f7345be501a30ddc83
Assumptions:
You don't need to match the key value; just the URL-path (ie. /pwreset.php)
example1.com and example2.com point to different places (the filesystem does not overlap).
To redirect from 1. to 2. try the following at the top of your .htaccess file in the root of example1.com:
RewriteEngine On
RewriteRule ^pwreset\.php$ https://example2.com/support%{REQUEST_URI} [R=302,L]
Any query string (eg. key=abc...) is passed through unaltered.
Note that this is a 302 (temporary) redirect. Only change it to a 301 (permanent) when you have confirmed it works OK.
If you need to check that a key= URL param is present and is set to a 32 hex string (which appears to be what your example represents) then include a condition before the above RewriteRule that checks against the QUERY_STRING server variable. For example:
RewriteCond %{QUERY_STRING} ^key=[0-9a-f]{32}$
RewriteRule ^pwreset\.php$ https://example2.com/support%{REQUEST_URI} [R=302,L]
If any other URL params are present on the request then the redirect will fail.
Aside:
RewriteRule ^(.+?)/$ https\:\/\/www\.example\.com\/support\/pwreset\.php [L]
This looks very cPanel-esque. There is no need to backslash colons, slashes and dots in the RewriteRule susbtitution argument. This is an "ordinary" string, not a regex. These characters have no special meaning here.
This seems easy enough but I can't seem to find the exact solution online and to get it to work. I want to redirect all the links e.g.
user1.example.com
user2.example.com
user3.example.com etc.
to
example.com/search.html?user=user1
example.com/search.html?user=user2
example.com/search.html?user=user3 etc.
in my .htaccess I have
RewriteCond %{HTTP_HOST} !^(www\.)?example.com$ [NC]
RewriteCond %{HTTP_HOST} ^([\.]+)\.example.com$ [NC]
RewriteRule .* /search.html?user=% [L]
I want to redirect all the user "subdomains" (they don't really exist on the server) except if the domain is prefix with www. The "user" field consists of alphabets, numeric, - (dash), _ (underscore) and possibly space. I am getting the Server Not Found error with the above .htaccess with for example, user1.example.com What am I doing wrong? Can this be done without the real existence of these third level domains? Thanks in advance.
You can use negative lookahead in your condition while capturing first part of domain and use the back-reference as %1 later:
RewriteCond %{HTTP_HOST} ^((?!www\.)[^.]+)\.example\.com$ [NC]
RewriteRule !^search\.html$ /search.html?user=%1 [L,QSA,NC]
Try this :
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com$ [NC]
RewriteRule .* /search.html?user=%1 [R,L]
I have a set of URLs that are in the form www.website.co.uk/businessname
and these are meant to do a htaccess Proxy redirect to pull data from another domain on the server.
This is what I have:
#Start Business pages
RewriteCond %{HTTP_HOST}%{REQUEST_URI} ^www.outputside.org.uk/swellcottage$
RewriteRule ^(.*) http://www.datasite.co.uk/$1 [P]
RewriteCond %{HTTP_HOST}%{REQUEST_URI} ^www.outputsite.org.uk/hawthorn-books$
RewriteRule ^(.*) http://www.datasite.co.uk/$1 [P]
#End Business Pages
The first rule works perfectly, displaying the contents of http://www.datasite.co.uk/swellcottage on the URL www.outputsite.org.uk/swellcottage but the second rule/condition doesn't work.
I looked at escaping the - character in the URL as so:
RewriteCond %{HTTP_HOST}%{REQUEST_URI} ^www.outputsite.org.uk/hawthorn\-books$
RewriteRule ^(.*) http://www.datasite.co.uk/$1 [P]
#End Business Pages
But this still doesn't seem to engage correctly, the rewrite rules do not work and the site replies with the fallback 404 that the page www.outputsite.org.uk/hawthorn-books does not exist.
Where else and how else should I escape the dash or otherwise what's wrong with my syntax here? cheers
ps. I have it that filenames (index.php) and folder names (/places/) are not subject to the proxy redirects .
UPDATE
I have more Rules in my htaccess:
#to add www. to each request.
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301]
#Start biz pages
...
#End biz Pages
#to prevent error looping
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule .* - [L]
I have also run the same code above with hawthornbooks without the dash, escaped or otherwise and the code works correctly, so I need to find out the correct way of escaping the dash character. All code and work is UTF-8.
NOTE: This issue appears similar to Why if I put a "-" dash in a rule in my htaccess doesn't work? but I found no solution to that problem.
If problem is just a hyphen this should work:
#Start Business pages
RewriteCond %{HTTP_HOST} ^(www\.)?outputside\.org\.uk$ [NC]
RewriteRule ^(swellcottage)/?$ http://www.datasite.co.uk/$1 [P,NC]
RewriteCond %{HTTP_HOST} ^(www\.)?outputside\.org\.uk$ [NC]
RewriteRule ^(hawthorn.books)/?$ http://www.datasite.co.uk/$1 [P,NC]
#End Business Pages
This is intended to be a temporary rather than a permanent solution, but you might try the following rule:
RewriteCond %{HTTP_HOST}%{REQUEST_URI} ^www.outputsite.org.uk/hawthorn([^\/]*)\/?$
RewriteRule ^(.*) http://www.datasite.co.uk/$1 [P]
This will look for any URI which conforms to the pattern www.outputsite.org.uk/hawthorn followed by any number of characters (or zero characters) which aren't / and then ending with an (optional) /.
RewriteCond %{HTTP_HOST} ^(www.)?example\.com$ [NC]
RewriteRule ^(.+)/$ http://%{HTTP_HOST}/$1 [R=301,L]
So i have this in my htaccess file to take care of the trailing slash problem . It redirects you every time you add a trailing slash on a url.
The issue here is the fact there is one directory where it needs the trailing slash or it breaks. How do I add a exception for a directory like http://www.example.com/com/ to this rule..
The simplest to understand approach is to use an additional condition:
RewriteCond $1 !=com
RewriteCond %{HTTP_HOST} ^(www.)?example\.com$ [NC]
RewriteRule ^(.+)/$ http://%{HTTP_HOST}/$1 [R=301,L]
This works because the order of execution is rule.regexp, cond1, cond2, rule.substitution. Hence the $1 variable is available to the (new) cond1. You could also use a negative assertion on your regexp.
Incidentally, the http://%{HTTP_HOST}/ is assumed in the case of a [R=301] for http. Why do you not want to remove / for other host aliases?
basically need to convert
with www or not, example.com/[anycharacter]
into
with www or not, example.com/cgi-bin/new-disk.cgi/dir/smooth/[anycharacter]
additinoally...
i would like to redirect ALL www.example.com into example.com
This should work for you:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !cgi-bin/new-disk.cgi/dir/smooth
RewriteRule ^(.*)$ /cgi-bin/new-disk.cgi/dir/smooth/$1 [L]
For the first two lines, it checks to see if you have www in your URL. If so, bounce it to the non-www version.
Note the exclamation mark (!) on the second last line. This is a not operator and in this test, is checking to see if your currently requested file isn't your final rewriting file, in this case:
cgi-bin/new-disk.cgi/dir/smooth
If that's true, shunt it to the actual rewrite script you have as pointed out in the final line.
The character, $1, references the first capture group as marked by the first set of parentheses on the same line.
For part of your answer, I believe you can use this as an example to base off.. hopefully you use a test domain:
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule ^/(.*) http://example.com/$1 [L,R=301]
Might need to add a (.*) and make it optional for the other part.