Regex of a RewriteRule isn't working - apache

I'm actually using for the first time the RewriteRule command in a .htaccess, and I'm stuck...
What I'm trying to do is to make such redirection:
localhost/tchat/test_character.swf -> localhost/tchat/characters/test_character.swf
I'm using this current .htacces, as I don't know really where I was supposed to place it, I placed it in / and a copy in /tchat.
RewriteEngine On
RewriteRule ^tchat/([^_/])_character\.swf$ /tchat/character/$1_character.swf [R=301]
But when I try to access the /tchat/test_character.swf I just get a 404...
I think the problem is in my regex but I just can't figure where inside the regex. I've already tried to add a RewriteBase / after the RewriteEngine On, and I also tried to add a / beetwen the ^ and tchat. But it doesn't worked.
So I'm really curious to know where the regex fail...
Thank's a lot!
Ps: Please excuse my English, It's not my native language :/

Finally, it worked by changing the htaccess to:
RewriteEngine On
RewriteBase /tchat
RewriteRule ^([^_/]+)_character\.swf$ /tchat/character/$1_character.swf [R=301]

Related

Simple rewrite not working (.htaccess)

I'm trying to work out a bigger problem, which I believe lies in my .htaccess file. So I stripped it down to the following for testing purposes:
RewriteEngine on
RewriteRule ^sandbox/htaccess/one.php$ sandbox/htaccess/two.php
I'm simply trying to get requests for
http://example.com/sandbox/htaccess/one.php
to go to
http://example.com/sandbox/htaccess/two.php
However, this is not working. I simply see one.php.
I can confirm that .htaccess file is being read.
What am I missing to get this simple example working?
Try this, it will work
RewriteEngine on
RewriteBase /
RewriteRule ^sandbox/htaccess/one\.php$ /sandbox/htaccess/two.php? [L,R=301]

.htaccess url parameter rewrite for specific page

I have been battling for hours now and searched Google and StackOverflow, without a solution to my problem, hope you guys can help! :-)
I have a page with parameters as follows: profile?userid=123456789. I want it to rewrite to profile/123456789. This I have, but now everywhere on this page all links gets /profile/link. Is there any fixes to this? Thanks in advance, here comes my .htaccess code.
RewriteEngine on
RewriteCond %{REQUEST_URI} !/profile\.php
RewriteRule ^profile/([^/]*)$ /profile?userid=$1 [L,QSA]
try this, it works for me with many configurations
RewriteEngine On
RewriteRule ^profile/([^/]*)$ /profile?id=$1 [L]
if you want the ([^/]*) to be exclusively number, you'll need to use PHP to rewrite your urls
You can change your rule parameter order as below to make it work
RewriteEngine on
RewriteRule ^profile?userid=([^/]*)$ profile/$1 [L]
Hope this will help you :)

Apache2 rewrite strange behaviours

For a newsletter I'm trying to make clean url's for the unsubscribe page
At first I was having this, but it wasn't working:
RewriteEngine On
RewriteBase /
RewriteRule ^/unsubscribe/(.*)$ /unsubscribe.php?email=$1 [NC,L]
Which is very strange in my opinion. So I tried some other methods and ended up with the following:
RewriteEngine On
RewriteBase /
RewriteRule ^/?unsubscribe/?(.*)$ /unsubscribe.php?email=$1 [NC,L]
This, in fact, is working. But it is giving very strange results.
The email parameter value is now:
.php/test
It is driving me nuts because I don't see why it is behaving this way.
Is there anybody who has an idea on what is happening here and how to get it fixed?
I'd rather not end up string replacing the php, there is something wrong here.
How are you my friend ? :)
Could you try this and let me know if it works? Let's figure this out!
RewriteEngine On
RewriteRule ^unsubscribe/([^/.]+)/?$ unsubscribe.php?email=$1 [L]
Your first Rule doesn't work, because the rule gets what's BEHIND the base in the URL. So if the URL is /unsubscribe/my#email, the RewriteBase (/) gets removed, and the RewriteRule will see unsubscribe/my#email without the leading /.
In your second rule, BOTH /-es are made optional by the ?. So unsubscribe.php/test will find the literal unsubscribe, and take everything behind it - .php/test - and put it in the email parameter. Guess you should use /unsubscribe/test, not /unsubscribe.php/test in your browser when testing.
This rule should work for you:
Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteRule ^/?unsubscribe(?:/(.+))?$ /unsubscribe.php?email=$1 [NC,L,QSA]
Problem was in your regex which was matching the pattern after first application and doing the rewrite twice.
Ok got it worked out.
It seemed like MultiViews (whatever that may be) was enabled by default.
Adding:
Options -MultiViews
To the htaccess (or apache config) fixed the problem.

.htaccess RewriteRule using # sign?

I am trying to have a URL that would look like:
mysite.com/#username
Which would route to something like:
subdomain.mysite.com/user/#username
I'm terrible with RewriteRules and have been struggling to try to get this to work. Some of the things I have tried are:
RewriteRule subdomain.mysite.com/user/(.*) mysite.com/$1 [R=302,NC,L]
RewriteRule ^(.#)(.*) subdomain.mysite.com/user/$1 [R=302,NC]
RewriteRule ^(.#)([A-Za-z0-9_]+) mysite.com/user/$1 [R=302,NC]
I realize those probably make absolutely no sense. Every time I try to get my head around how the routing works, I get turned around and start writing crap like you see above.
Any pointers would be appreciated. Thanks.
You can't route to a subdomain (using htaccess anyway) but you can redirect there.
RewriteEngine On
RewriteBase /
RewriteRule #(.*) http://subdomain.mysite.com/user/#$1 [L,R=301]
The # symbol should be okay https://stackoverflow.com/a/1547940/763468

Rewriting url using mod-rewrite and apache

I'm trying to add some rewriting on my site, but it seems to not work, I'm using apache and .htaccess.
The code in my .htaccess file is:
RewriteEngine On
RewriteRule ^/?os_framework/?$ /os_framework/index.php?module=home [L,NC,QSA,PT]
This should send http://localhost/os_framework/ to http://localhost/os_framework/index.php?module=home
But it seems not to.
Any help would be appreciated.
In advance, thanks
Edit: Fixed the above, shouldn't have the os_framework/ in the search pattern, however now i cant get this one to work:
RewriteRule ^/(.[^/]*)/?$ /os_framework/index.php?module=$1 [L,NC,QSA,PT]
And what is wrong with
RewriteRule ^(.[^/]*)/?$ /os_framework/index.php?module=$1 [L,NC,QSA,PT]
Why does that throw a error 500? it should work
Try this:
RewriteEngine On
RewriteRule ^os_framework/?$ os_framework/index.php?module=home [L,NC,QSA]