htaccess dash in rewrite causing issues - .htaccess

I cant seem to get my htaccess code right for redirecting pages that appear like the below to go to a 410 page, eg:
www.domain.com/-c-23.html
www.domain.com/-c-12.html
www.domain.com/-c-755.html
Basically, I want a rule whereby anything where "-c-" comes directly after the slash of the domain gets sent to a 410. I have been trying to do this but my code isn't working, so far I have something like this:
RewriteRule ^/\-c-[0-9]+\.html$ - [G]
But this makes no difference at all, any idea why this is not working?

You need to get rid of the leading slash in your regular expression. URI's sent through rules in htacccess files have the leading slash stripped off:
RewriteRule ^\-c-[0-9]+\.html$ - [G,L]
You also need the L flag to immediately stop rewriting. You could also be even more general:
RewriteRule ^-c- - [G,L]

Related

too many redirects. setting up htaccess to redirect

I moved an old website to a new cms and some content have links that cannot be found on the new system.
For instance in the old system there was a link called https://example.com/newsletter/1234/123
in the new one i do not require those ids at the end. Therefore I would like to redirect the user to
https://example.com/newsletter/ directly.
I wrote the following in my htaccess file:
RewriteRule ^(.*newsletter)(.*) $1 [L,R=301]
This gives me unfortnately a "too man redirects" error.
Can anyone point out the error that I made?
I planned to use the regex to capture all links that contain the name "newsletter" and remove the rest of the url to redirect the user. Any help is appreciated thanks.
Using (.*newsletter)(.*) causes the rewrite to match /newsletter alone because .* matches zero or more characters. This sends mod_rewrite into a loop.
Instead, you can use .+ which matches one or more characters, and to that I would prepend /? to optionally match a trailing slash. There is no need to capture it in () because you do not reuse it as $2.
RewriteRule ^(.*newsletter)/?.+ $1 [L,R=301]
Your example began with .*. If you actually need that, leave it in. But if you are really just trying to match /newsletter/123/1234 and not /someother/newsletter/123/1234, simplify it with:
RewriteRule ^newsletter/?.+ /newsletter [L,R=301]
When you test this, use a private/incognito browsing window or a fresh browser. Browsers aggressively cache 301 redirects and it can make it very hard to debug rewrite rules if you are fighting against the cache.

Why doesnt this htaccess rewrite work?

Okay so I am trying to make it so that if people go to /?char=USERNAME it would show the contents of /game/CharWidget.swf?login=USERNAME. This is my code so far:
RewriteEngine on
RewriteCond %{QUERY_STRING} char=(.*)
RewriteRule ^index.php?char=(.*) /game/CharWidget.swf?login=%1
This makes the url server side as /game/CharWidget.swf but doesn't carry the ?char=username and make it ?login=username so it wont show what I want it to show.
Edit; If it's easier doing /char/USERNAME to /game/CharWidget.swf?login=USERNAME i wouldnt mind doing that if someone could give me the code for it.
The query string is not visible to RewriteRules, so ^index.php?char=(.*) will never match. (Except that, since you haven't escaped . or ?, it will match e.g. indexZphchar=foo, which is probably not what you want.)
Also, if the user visits /?char=USERNAME, what the RewriteRule would normally see is just /; no index.php. Finally, if this is in an .htaccess file, you'll generally also need a RewriteBase directive.
Putting all those fixes together, something like this should work:
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} ^char=(.*)$
RewriteRule ^/?(index\.php)?$ /game/CharWidget.swf?login=%1 [NS]
(The regexp ^/?(index\.php)?$ will match either an empty path or index.php, with or without a leading slash. That makes it a bit more complex than absolutely necessary, but also more robust. In particular, the /? lets it also work outside .htaccess files, where the leading slash will be present.)
Ps. The regexp ^char=(.*)$ will also allow URLs like /?char=foo&bar=baz to be rewritten to /game/CharWidget.swf?login=foo&bar=baz. If you don't want to allow such rewrites, replace it with e.g. ^char=([^&;]*)$.
Edit: Unfortunately, this isn't going to work for .swf files, because those execute on the client, and so won't see any changes to the query string made by server-side rewrites.
What you could do is make the rewrite external by replacing the [NS] flag with [NS,L,R=302]. However, this will also change the URL shown in the browser address bar, which may not be what you want. If so, another option would be to make the original request serve an HTML page on which you embed the .swf file.

Using .htaccess to style URL directory style

I have searched this question and looked around but can't seem to get this working in practice. This is my .htaccess file:
Options +FollowSymLinks
RewriteEngine on
RewriteRule /poker/(.*)/(.*)/$ /poker/?$1=$2
I am trying to get my page to work like this:
mysite.com/poker/page/home
But this just isn't working, I have used 3 different generators and tried typing it manually from tutorials but it is just returning a 404. Any idea's a greatly appreciated, it could be really obvious..
Thanks
You do not have a trailing slash in your example, yet your rule requires one. You can make the trailing slash optional:
RewriteEngine on
RewriteRule /poker/(.*)/(.*)/?$ /poker/?$1=$2
Note however, that a uri /poker/a/b/c/d/e/f/g/ is also a match here - a/b/c/d/e/f will match the first subpattern and g will match the second one, because (.*) is greedy. Be more specific if you wish to match only content between slashes - e.g. ([^/]*)
Well, there's really nothing wrong with the rules that you have if http://mysite.com/poker/?page=home resolves correctly. The only thing is that if this is in an htaccess file, the leading slash is removed from the URI when it's matched against in a RewriteRule, so you need to remove it from your regular expression (or maky it optional):
RewriteRule ^poker/(.+)/(.+)/?$ /poker/?$1=$2
And maybe make the groupings (.+) instead so that there is at least one character there.

Removing unwanted characters from URL in htaccess

Our current htaccess setup correctly converts urls like this: site.com/page.php?sid=Friend to site.com/Friend
However, due to an unrelated oversight, we had almost all of our URLs double-indexed as site.com/Friend> Because the greater than sign is a special character it doesn't call page.php so the > needs to be stripped out in htaccess and can't be done on page.php. Compounding matters is that the way they're indexed is as: site.com/Friend%3E which also might need to be stripped out.
What we would like is to have another directive that looks for an ending of > (or %3E), strips it off, then redirects to the variable that's there without that ending > In essence so that site.com/Friend> (or site.com/Friend%3E) still points to site.com/Friend
Thank you for your help.
Add this to the top of your rules:
RewriteRule ^/?(.*)>$ /$1 [L,R=301]
You can use > because the URI gets decoded when matching in a RewriteRule.

ISAPI Rewrite Help With Redirect To Sub Domain

I need a bit of ISAPI syntax help, I am about to put live a new site and want to archive the old forum onto an archive sub domain.
The forum is in ASP and currently has this URL
http://www.mywebsite.co.uk/forum/forum_posts.asp?TID=34419
http://www.mywebsite.co.uk/forum/forum_topics.asp?FID=47
I want every request for the forum to be 301 redirected to:
http://archive.mywebsite.co.uk/forum/forum_posts.asp?TID=34419
http://archive.mywebsite.co.uk/forum/forum_topics.asp?FID=47
Basically anything with in the forum folder with .asp extension with or without a query string - Any help greatly appreciated.
Thanks
For redirecting a url to a subdomain, it has been a few months since your question, but maybe this will still help.
Assuming you're using isapi_rewrite v3, try this:
RewriteCond %{HTTP:Host} ^www\.
RewriteRule ^/forum/(.*\.asp)$ http://archive.mywebsite.co.uk/forum/$1 [NC,R=301]
The first line looks for host beginning with www. (with the trailing dot). This makes sure you don't get an infinite loop, redirecting archive to itself. The trailing dot is being picky at doing only www, and not others like www2.
The second line looks for /forum/, then captures (...) any characters .* and literal dot and asp \.asp, ending the url $
It then goes to your subdomain in the /forum/ folder (since /forum/ wasn't captured, we need to repeat it), and the entire rest of the url that was captured $1.
The NC means not case-sensitive, so all this can be mixed upper and lower case.
The R=301 means redirect, and make it a permanent 301 redirect since this isn't temporary.
In the v3 rules, querystring parameters are handled entirely separately from the rules, so you don't have to do anything at all here. If there are no parameters, then this works. If there are parameters, they are passed on as in your question above.
This ignores http vs https. It will redirect to http, so if the original was https, there will probably be a browser warning. There are ways to handle it, but I didn't want to clutter the basic answer.
Having the domain itself in the rewrite rule is always a little weird looking to me, since you might want to move it around. You can capture the rest of the host in the first line, and use it in the second.
RewriteCond %{HTTP:Host} ^www\.(.*)$
RewriteRule ^/forum/(.*\.asp)$ http://archive.%1/forum/$1 [NC,R=301]
This is similar to above, with the addition that the host after the www-dot is captured, to the end of the line (.*)$ I'm not sure the $ is required here, but it makes it explicit we want it all.
RewriteCond captures are numbered with a percent sign, so %1 in the rewrite rule substitutes the host after the subdomain, and $1 still means substituting the captured ...asp url.

Resources