Simple rewrite not working (.htaccess) - apache

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]

Related

.htaccess simple mod_rewrite sends 404 error

I've created an .htaccess file in a shared server, inside a folder called API that is a child of the root folder.
I've placed the following simple code inside the file
Options +FollowSymLinks
RewriteEngine On
RewriteBase /API/ # also tested with / alone (edit) also tested removing-it
RewriteRule ^/API/(\d+)*$ /API/index.php?i=$1
My intentions is to turn /API/{var} into /API/index.php?i={var}, but the trick ain't working at all. What can be causing the issue, since every query var I sends me into 404.
What can be causing the problem? Where should I start debugging?
Edit:
After several failed attempts I'm gonna try using the FastRoute php library as an alternative, since this .htaccess issue seems unresolvable.
Thanks FĂ©lix for all the help in the chat.
The problem is probably the leading slash / and API in your rewrite pattern, Remove them from there
Try :
Options +FollowSymLinks
RewriteEngine On
RewriteBase /API/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(\d+)*$ /API/index.php?i=$1 [NC,L]

Apache htaccess rewrite (Pretty URLs)

I have a sneaking suspicion this is not possible, but figured I would ask regardless.
Is it at all possible to take a URL passed to a server in the form of:
http://domain.com/index.php?Action=Controller/Action&one=1&two=2&three=3
And rewrite it to appear as:
http://domain.com/Controller/Action/1/2/3
I am trying to clean up an borderline ancient project to support "Pretty URLs" and I would really like to make the URLs display a bit nicer. I know I could setup a 301 header redirect to the new URL, but I would prefer to avoid that overhead if at all possible.
Any thoughts?
Thanks!
To get
http://domain.com/index.php?Action=Controller/Action&one=1&two=2&three=3
To appear as
http://domain.com/Controller/Action/1/2/3
You will need to use %{QUERY_STRING} to capture the query string data. Your .htaccess file will look like this:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^Action=Controller/Action&one=(\d+)&two=(\d+)&three=(\d+)
RewriteRule ^.+ /Controller/Action/%1/%2/%3 [R=301,L]
This will set up a permanent redirect to the new page. You can play around and test .htaccess rewrite rules here: htaccess.madewithlove.be
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteBase /
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ index.php?Action=$1/$2&one=$3&two=$4&three=$5 [L,QSA]

Regex of a RewriteRule isn't working

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]

.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

Using mod_rewrite correctly on Debian server

the good old mod_rewrite. I can't seem to get it right.
Typical scenario: A user types in "http://domain.com/page"
I want that the user is being redirected to "http://domain.com/page/page2"
My htaccess file looks as follows:
RewriteEngine on
RewriteBase /var/www/
RewriteRule ^/page/$ page/page2
RewriteRule ^/bla/$ page/page2/bla
The first rewrite rule works, the second on the other hand doesn't seem to have any effect. Any idea? Maybe a better way to do this?
And another question:
As I said the first rewrite works just fine, but the url is not pretty. "http://domain.com/page" changes to "http://domain.com/page/page2". Is there a way to keep the typed in url but still forward the user to the actual link?
I presume the .htaccess is in your DocumentRoot.
How does your /bla containing look like? This should not rewrite the URL in the browser.
Use this:
RewriteEngine on
RewriteBase /
RewriteRule ^(/?)page/?$ $1page/page2 [L]
RewriteRule ^(/?)bla/?$ $1page/page2/bla [L]