Why doesnt this htaccess rewrite work? - .htaccess

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.

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.

mod_rewrite .htaccess with %20 translate to -

I have been reading about .htaccess files for a couple of hours now and I think I'm starting to get the idea but I still need some help. I found various answers around SO but still unsure how to do this.
As far as I understand you write a rule for each page extension you want to 'prettify', so if you have something.php , anotherpage.php, thispage.php etc and they are expecting(will receive??) arguments, each needs its own rule. Is this correct?
The site I want to change has urls like this,
maindomain.com/sue.php?r=word1%20word2
and at least one page with two arguments
maindomain.com/kevin.php?r=place%20name&c=person%20name
So what I would like to make is
maindomain.com/sue/word1-word2/
maindomain.com/kevin/place-name/person-name/
Keeping this .php page and making it look like the directory. Most of the tutorials I have read deal with how to remove the .php page to which the argument is passed. But I want to keep it.
the problem I am forseeing is that all of the .php?r=parts of the url are the same ie sue.php?r=, kevin.php?r= and the .htaccess decides which URL to change based on the filename and then omits it. If I want to keep the file name will I have to change the ?r=
so that it is individual? I hope this make sense. So far I have this, but I'm sure it won't work.
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^([a-zA-Z0-9]+)/$1.php?r=$1
RewriteRule ^([a-zA-Z0-9]+)/$1.php?r=$1&c=$1
And I think I have to add ([^-]*) this in some part or some way so that it detects the %20 part of the URL, but then how do I convert it to -. Also, how are my $_GET functions going to work??
I hope my question makes sense
You're missing a space somewhere in those rules, but I think you've got the right idea in making 2 separate rules. The harder problem is converting all the - to spaces. Let's start with the conversion to GET variables:
# check that the "sue.php" actually exists:
RewriteCond %{REQUEST_URI} ^/([a-zA-Z0-9]+)/([^/]+)/?$
RewriteCond %{DOCUMENT_ROOT}/%1.php -f
RewriteRule ^([a-zA-Z0-9]+)/([^/]+)/?$ /$1.php?r=$2 [L,QSA]
RewriteCond %{REQUEST_URI} ^/([a-zA-Z0-9]+)/([^/]+)/([^/]+)/?$
RewriteCond %{DOCUMENT_ROOT}/%1.php -f
RewriteRule ^([a-zA-Z0-9]+)/([^/]+)/([^/]+)/?$ /$1.php?r=$2&c=$3 [L,QSA]
Those will take a URI that looks like /sue/blah/ and:
Extract the sue part
Check that /document_root/sue.php actually exists
rewrite /sue/blah/ to /sue.php?r=blah
Same thing applies to 2 word URI's
Something like /kevin/foo/bar/:
Extract the kevin part
Check that /document_root/kevin.php actually exists
3 rewrite /kevin/foo/bar/ to /kevin.php?r=foo&c=bar
Now, to get rid of the "-" and change them to spaces:
RewriteCond %{QUERY_STRING} ^(.*)(c|r)=([^&]+)-(.*)$
RewriteRule ^(.*)$ /$1?%1%2=%3\ %4 [L]
This looks a little messy but the condition matches the query string, looks for a c= or r= in the query string, matches against a - in the value of a c= or r=, then rewrites the query string to replace the - with a (note that the space gets encoded as a %20). This will remove all the - instances in the values of the GET parameters c and r and replace them with a space.

htaccess dash in rewrite causing issues

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]

Mod_Rewrite to /subdirectory and /subdirectory/query

I'm having a difficult time getting into using mod_rewrite. I've been at this for about an hour googling stuff but nothing quite seems to work. What I want to do is change
example.com/species.php into example.com/species
and also
example.com/species.php?name=frog into example.com/species/frog.
Using
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^species/(.*)$ /species.php?name=$1
I can get example.com/species.php?name=frog to display as example.com/species/frog, and with
RewriteRule ^species/ /species.php
I can get example.com/species.php to display as example.com/species/, but I can't get both of them to work at the same time.
Also, example.com/species with no trailing slash always comes up as a 404.
I've considered just making a /species/ directory to catch any problems but I'd rather just have a few rules for one species.php file. Any help would gladly be appreciated!
Edit (because I can't answer my own question for 8 more hours):
I seem to have fixed both of my problems. I changed my .htaccess to:
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^species/(.*)$ /species.php?name=$1
RewriteRule ^species/?$ /species.php
The second RewriteRule successfully redirects example.com/species to example.com/species.php while leaving the other RewriteRule working at the same time.
However, if I typed in example.com/species/ with a trailing slash, it was being read as example.com/species.php?name= and would throw an error because no name was submitted, so I just added
if(isset($_GET['name']) && empty($_GET['name'])) {header('location: http://example.com/species');}
so that if I used example.com/species/ it would redirect to /species and work as desired.
If you change the * (match zero or more) to a + (match one or more) in your first RewriteRule then you should stop seeing species.php?name= if a trailing slash is used.
This is because the + will require that something appears after the slash, otherwise the rule will not match. Then your second RewriteRule will match because it ends with an optional slash, but will not add the name= query string to the target URL.
You may also want to add the [L] flag (last) after the first rule, because you don't need the second rule to execute if the first rule matches. (Note that this will not stop the RewriteCond and RewriteRule tests being run on the resulting redirect URL, which will have to go through the .htaccess file just like any other request.)
See the Reference Documentation for mod_rewrite in Apache 2.4 (or see the docs for the version of Apache you're actually using).

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