how do i fix this rewrite URL rule? - .htaccess

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.

Related

Redirect in .htaccess makes me cracy

Following ws call https://training-deluxe.de/nlpdocs/podcast/feed/
should be redirected to podcast hoster podigee
.htaccess
RewriteEngine On
RewriteCond %{SERVER_PORT} !=443
RewriteRule ^(.*)$ https://www.training-deluxe.de/$1 [R=301,L]
RewriteCond %{SERVER_PORT} !^443$
#Whats wrong with the next line
RewriteRule /nlpdocs/podcast/feed/ https://coachingundwissenschaft.podigee.io/feed/mp3 [R=301,L]
RewriteRule /coaching_ausbildung/gesundheitscoach_somatic_release_achtsamkeit.html https://rubin-institut.de/health-practitioner-und-gesundheitscoach/ [L,R=301]
RewriteRule ^(nlpdocs/.*)$ https://www.rubin-institut.de/$1 [R=301,L]
RewriteRule ^(.*)$ https://rubin-institut.de/$1 [L,R=301]
Redirect goes to rubin-institut/nlpdocs/podcast...
I cant get the clue
There's a couple of issues...
RewriteEngine On
RewriteCond %{SERVER_PORT} !=443
RewriteRule ^(.*)$ https://www.training-deluxe.de/$1 [R=301,L]
RewriteCond %{SERVER_PORT} !^443$
You need to remove that 2nd/last RewriteCond directive above as that will break the rule that follows. RewriteCond directives are conditions that apply to the first RewriteRule directive that follows.
#Whats wrong with the next line
RewriteRule /nlpdocs/podcast/feed/ https://coachingundwissenschaft.podigee.io/feed/mp3 [R=301,L]
The first argument to the RewriteRule directive takes a regular expression (regex) - as you've used in later rules. It is not a simple URL-path. And, importantly, in .htaccess the URL-path matched by the RewriteRule pattern does not start with a slash. (You have omitted the slash prefix in the later rule that is evidentally "working".)
It should be like this instead:
RewriteRule ^nlpdocs/podcast/feed/$ https://coachingundwissenschaft.podigee.io/feed/mp3 [R=301,L]
You will need to clear your browser cache before testing since the erroneous 301 (permanent) redirect will have been cached by the browser. Test first with 302 (temporary) redirects to avoid caching issues.
You will also need to check the rule that follows, as that looks like it would have the same problem.
Reference:
https://httpd.apache.org/docs/current/rewrite/intro.html
https://httpd.apache.org/docs/current/mod/mod_rewrite.html

.htaccess to redirect to new website structure

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]

Htaccess - Rewrite engine (reverse engineering a line of code)

On a site I'm working on, if you enter the url, plus 1 directory, the htaccess adds a trailing slash.
So, this: http://www.mysite.com/shirts
Becomes this: http://www.mysite.com/shirts/
The htaccess that runs the site is quite long and complex, so it's not easy to find or test which rule is causing the rewrite. I was able to track down the issue to this line of code (I think):
RewriteRule (.*) http://www.mysite.com/$1 [R=301,L]
Does this rule match the behavior I'm describing above? It seems to be the cause, but it doesn't make logical sense to me. I don't unsderstand where the trailing slash is coming from.
Can someone shed some light on this for me? Thanks in advance.
Edit: MORE:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^mysite\.com$
RewriteRule (.*) http://www.mysite.com/$1 [R=301,L]
By default apache will add the ending /, you will have to use:
DirectorySlash Off
To disable that behavior which is caused by mod_dir, you can read more about it here.
However if you're trying to remove the / to fix images not showing. That is not the right way to do it, you should instead use the HTML base tag, for example:
<BASE href="http://www.yourdomain.com/">
Read more here about it.
Your current rule as you have updated on your question:
RewriteCond %{HTTP_HOST} ^mysite\.com$
RewriteRule (.*) http://www.mysite.com/$1 [R=301,L]
Means:
if domain on the URL is only mysite.com
redirect current URL to domain with www.
So an example of it would be, if you access:
http://domain.com/blog/some_blog_article
It will redirect the user to:
http://www.domain.com/blog/some_blog_article
Note how it retains everything and only add the www. to the domain.
If you really want to redirect it regardless here is one way to do it:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^mysite\.com$ [NC]
RewriteRule (.*) http://www.mysite.com/$1 [R=301,L]
# check if it is a directory
RewriteCond %{REQUEST_FILENAME} -d
# check if the ending `/` is missing and redirect with slash
RewriteRule ^(.*[^/])$ /$1/ [R=301,L]
# if file or directory does not exist
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
# and we still want to append the `/` at the end
RewriteRule ^(.*[^/])$ /$1/ [R=301,L]

Skip a directory with htaccess

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?

Creating a mod_rewrite rule in an htaccess file

I am trying to create a mod_rewrite rule to direct people to a sub-folder. Currently the code looks as follows:
RewriteEngine On
RewriteCond %{HTTP_HOST} abcsite.com$ [OR,NC]
RewriteCond %{HTTP_HOST} ^!www\.abcsite\.*$
RewriteCond %{REQUEST_URI} !^/abc/.*$
RewriteRule (.*)$ /abc/$1 [L]
The redirect works if the user types www.abcsite.com, but not if they type abc.com. Is there something that I am missing or should do differently to make sure the user goes to the correct folder (regardless of how they type the URL)?
Side note: The htaccess file that I am dealing with is a Joomla file, so all contents of it deal with another Joomla site. I appreciate the help.
Because you have conditions for that.
RewriteCond %{HTTP_HOST} abcsite.com$ [OR,NC]
RewriteCond %{HTTP_HOST} ^!www\.abcsite\.*$
RewriteCond %{REQUEST_URI} !^/abc/.*$
All above rules will pass only its abcsite.com
You add following rules also then it work for abc.com too.
RewriteCond %{HTTP_HOST} abc.com$ [OR,NC]
RewriteCond %{HTTP_HOST} ^!www\.abc\.*$
RewriteCond %{REQUEST_URI} !^/abc/.*$
RewriteRule (.*)$ /abc/$1 [L]
There's a stray ! in your second condition. A ! in front of the pattern means that the condition is true when the regex doesn't match (like in the third condition). A ! inside the pattern is just a literal symbol.
The host conditions should be something like:
RewriteCond %{HTTP_HOST} ^abcsite\.com$ [OR,NC]
RewriteCond %{HTTP_HOST} ^www\.abcsite\.com$ [NC]
And in fact, they can be joined into a single condition (note, no [OR] here):
RewriteCond %{HTTP_HOST} ^(www\.)?abcsite\.com$ [NC]
Your third condition is intended to prevent redirect loops (/foo → /abc/foo → /abc/abc/foo → …). What it says is that the rule isn't applied if the request URL starts with /abc/. However, your actual redirect is an internal redirect: if a user accesses abcsite.com/foo, the server internally rewrites this to /webroot/abc/foo, but REQUEST_URI stays the same, /foo.
The reason this doesn't cause a redirect loop as it is is likely rewrite rules in abc/.htaccess which override this one once the redirect is done.
What should be checked instead in the third condition is the path matched by the rewrite rule:
RewriteCond $1 !^abc/
RewriteRule (.*) /abc/$1 [L]

Resources