.htaccess RewriteRule for subdirectory - apache

I have site e.g. 123.123.123.123/subdirectory/ and I would like to rewrite
123.123.123.123/subdirectory/page/123
to
123.123.123.123/subdirectory/page.php?id=123.
I've created .htaccess file:
RewriteEngine On
RewriteBase /subdirectory
RewriteRule ^page/(.*)$ page.php?id=$1 [NC,L,QSA]
but it doesn't work (rewriting in on).

Use this rule and it will work.
RewriteRule ^123\.123\.123\.123/subdirectory/page/123$ /123.123.123.123/subdirectory/page.php?id=123&%{QUERY_STRING}

Related

.htaccess not working on ip/~username

Here is my case. I'm trying to use .htaccess for rewrite url, but no matter how I try, browser always show 500 error or 404 error.
here is my code.
RewriteEngine On
RewriteBase /~torinots/beta/
RewriteCond %{REQUEST_FILENAME} !^(/beta/home)
RewriteRule ^home$ index.php [L]
example path: http://xx.xx.xx.xx/~username/beta/
Pls advice.
Update
I found this work!
RewriteEngine On
RewriteBase /~torinots
RewriteRule ^beta/home/?$ beta/index.php [L,NC]
I assume you have the .htaccess file in /~username/beta, since you use paths relative to that in the example above. Having a simple rule like the following rule will correctly internally rewrite the url, assuming that no other rules in .htaccess files higher up are interfering.
RewriteRule ^home$ index.php [L]
If you want to redirect a request to index.php too, then you need to prevent an infinite loop from happening. You can either use the END-flag (version 2.3.9 and up) or the THE_REQUEST trick.
#internal rewrite, and then stop everything
RewriteRule ^home$ index.php [END]
#Rewritebase is possibly needed for the redirect
RewriteBase /~torinots/beta/
#external redirect
RewriteRule ^index\.php$ home [R=301,L]
or:
#internal rewrite, and then stop everything
RewriteRule ^home$ index.php [L]
#Rewritebase is possibly needed for the redirect
RewriteBase /~torinots/beta/
#external redirect
RewriteCond %{THE_REQUEST} index\.php
RewriteRule ^index\.php$ home [R=301,L]

Redirect in .htaccess if file exists (cache)

I do not understand anything .htaccess and I need a redirection to cache.
Currently use the following rule:
RewriteRule ^movie/(.*)/(.*)/(.*)$ app.php?view=movie&theatre=$1&ordem=$2&movie=$3 [L,QSA]
That is, the requested URL
movie/jardins/0/robocop
redirects to
app.php?view=movie&theatre={jardins}&ordem={0}&movie={robocop}
But I want to redirect api/cache/html/{jardins}/{0}/{robocop}.html if file exists.
Recalling that {jardins} and {0} are directories.
How to do this? :(
You can have:
RewriteEngine On
RewriteBase /
RewriteCond %{DOCUMENT_ROOT}/app/cache/html/$1/$2/$3\.html -f [NC]
RewriteRule ^movie/(.*)/(.*)/(.*)$ app/cache/html/$1/$2/$3.html [L,NC]
RewriteRule ^movie/(.*)/(.*)/(.*)$ app.php?view=movie&theatre=$1&ordem=$2&movie=$3 [L,QSA]

.htaccess rewrite css/js/images to subfolder

I actually thought there should already be a thread about this but i wasn't able to find one.
let's say i have a website with this structure:
/index.php
/.htaccess
/template
/template/css
/template/css/foo.css
I'm redirecting everything to index.php with .htaccess
RewriteEngine on
RewriteBase /
RewriteRule ^(.*)$ index.php?0=$1 [QSA,L]
Now I've got a problem with the css. that i could fix by placing another .htaccess in /template with this
RewriteEngine off
Options -Indexes
And then access the css file with http://example.com/template/css/foo.css but actually i would prefere to have just http://example.com/css/foo.css
So i tried to put in /.htaccess the following code (that didn't worked). What did i do wrong?
RewriteEngine on
RewriteBase /
RewriteRule ^/css/(.*) www/css/$1 [L]
RewriteRule ^/js/(.*) www/js/$1 [L]
RewriteRule ^(.*)$ index.php?0=$1 [QSA,L]
note I don't have any .htaccess in /template!
You should remove the htaccess file in the css folder. You just need to exclude rewrites to the template directory. You can do it all in a single file:
RewriteEngine on
RewriteBase /
RewriteCond %{DOCUMENT_ROOT}/template/%{REQUEST_URI} -f
RewriteRule ^(.*)$ /template/$1 [L]
RewriteCond ${REQUEST_URI} ^/template/
RewriteRule ^(.*)$ index.php?0=$1 [QSA,L]
The first rule handles requests for the css directory, and the second rule is the one you had that routes to index.php except if the request is for template.

How do I rewrite this url with htaccess / mod_rewrite?

how do I rewrite this url with .htaccess mod_rewrite
http://55.100.10.66:81/var/class/tag?isAjax=true&id=189&key=eJwVxzEOwjAMB&callback=_prototypeJSONPCallback_0
to
http://55.100.10.66:81/index.php/var/class/tag?isAjax=true&id=189&key=eJwVxzEOwjAMB&callback=_prototypeJSONPCallback_0
Amongst a lot of things I tried the below which doesn't work
RewriteRule ^var/class/tag(.*) /index.php/var/class/tag$1 [L,NC,QSA]
Thanks!
This is the solution
RewriteCond %{REQUEST_URI} ^/var/class/tag(.*)
RewriteRule .* index.php [L]
You cannot match QUERY_STRING from RewriteRule. Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule ^(var/class/tag/?)$ /index.php/$1 [L,NC]

htaccess get folder one level below

I have some folders in root of my server:
css/
js/
gfx/
new_www/
...
and some more. Assuming that I want in folder new_www copy of current page, but force it to use some things from root folder (for example gfx folder). I've tried following with .htaccess but its not working:
RewriteEngine on
RewriteBase /
RewriteRule /new_www/_js/(.*) /_js/$1 [L]
RewriteRule /new_www/gfx/(.*) /gfx/$1 [L]
What am I doing wrong? .htaccess is placed in root of folder.
Please understand that RewriteRule doesn't match leading slash in an URI when used in .htaccess. Your code should be replaced with this:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule ^new_www/(_js/.*)$ /$1 [L,NC]
RewriteRule ^new_www/(gfx/.*)$ /$1 [L,NC]
OR even better will be to have just one rule like this:
RewriteRule ^new_www/(.+)$ /$1 [L,NC]