Rewrite Rule for subfolder instead of main website - php

I have a htaccess code that i want to be changed.
RewriteEngine on
RewriteBase /
RewriteRule ^(.*)$ proxy.php?url=$1 [L,QSA]
It works like a charm if i place proxy.php and .htaccess file in main website and visit pages through www.domain.com/
I want to change this RewriteRule in a way that if I place the proxy.php and htaccess in subfolder named "folder" then new results show in www.domain.com/folder/
So mainly purpose is to change results path from www.domain.com to www.domain.com/folder/

You can place your code without any changes in /folder/.htaccess with proxy.php.
Or you can use:
RewriteEngine on
RewriteBase /
RewriteRule ^folder/(.+)$ folder/proxy.php?url=$1 [L,QSA]

Related

how to rewrite single URL using .htaccess

I need to rewrite only 1 specific URL, to display to visitors specific content: I tried something like, this:
RewriteEngine on
RewriteCond %{REQUEST_URI} example.com/test/2_5/page.html
RewriteRule ^(.*)$ example.com/tt.html [R,L]
I need to rewrite all requests to:
http://example.com/test/2_5/page.html
to
http://example.com/tt.html
how to do this?
thanks,
Redirect /test/2_5/page.html /tt.html
Ok, mod_rewrite
RewriteRule ^/test/2_5/page.html /tt.html [L]
Remove first / if using .htaccess in the site's root folder, not the .conf file. And, typically, the final url should be the full one, with http:// and domain, but this one will work too. If you want to do everything by the rules then
RewriteRule ^/test/2_5/page\.html$ http://example.com/tt.html [L]

Url RewriteRule with php not able get the path [duplicate]

I have a single point of entry on my website
mysite.com/admin/index.php?View=List&Model=User
All access to the DB/application is through this index.php file.
I would like to clean my URL from what is shown above to.
mysite.com/admin/List/User
The idea is to remove the key 'Model and 'View'.
I have found this link that looks very similar.
PHP/Apache: Rewrite rules with .htaccess
in htaccess file insert this code :
RewriteEngine On
RewriteRule ^admin/([^/]*)/([^/]*)$ /admin/index.php?View=$1&Model=$2 [L]
The rewritten URL:
http://mysite.com/admin/List/User
this site very useful for generate rewrite URL
http://www.generateit.net/mod-rewrite/
Create a file named .htaccess in your web root if you haven't already got one with the following:
# Turn the rewrite engine on (skip this if already done)
RewriteEngine On
# Redirect admin/List/User/ to admin/index.php?View=List&Model=User
RewriteRule ^admin/([^/.]+)/([^/.]+)/?$ admin/index.php?View=$1&Model=$2 [L]
This might work:
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^admin/([^/]+)/([^/]+) /index.php?View=$1&Model=$2 [NC]
Add to .htaccess in the root folder of your website.

Apache and Url Rewrite

I have 2 main pages. It works like so,
<html>
<body>
<div id='menu'></div>
<div id='content'><?PHP require("content.php"); ?></div>
</body>
</html>
Then, content.php pulls the "page" from the url, and checks it through a url of "good pages". If not, there is a default. Anyway, the typical url looks like this
/index.php?page=user&id=1
/?page=group&id=3
I'm trying to make it look like this
/user/1
/group/3
This is what I have for my .htaccess file, which doesn't work 100%. It pulls the correct page, but the css files doesn't seem to be uploaded. It is in /css/ in the main directory as index and content.
EDIT
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteRule ^(.*)/(.*)$ /index.php?page=$1&id=$2 [L]
I'd like the application to be scale-able if possible.
Thanks!
You will want to be using either an absolute path to your CSS, or use a base href for it, or you can prefix the path to the CSS with a / if the path to starts from the root domain.
Your .htaccess is fine, but might want some slight tweak:
RewriteEngine On
RewriteRule ^(\w)/([0-9]+)/$ /index.php?page=$1&id=$2 [L,QSA]
please try with the following code you can get the values in the $_GET['page']. Then you can split the values by / and then can get your user name,id etc
RewriteEngine On
RewriteBase /project/
RewriteRule ^([A-Za-z0-9]+){0,1}$ index.php?page=%{REQUEST_URI} [L,NC,QSA]
In the above code you may want to replace the /project/ with /. It will work perfectly
Try this .htaccess code
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^(.*)/(.*)$ /index.php?page=$1&id=$2 [L]
You can now directly access your webpage via..
www.yourdomain.com/user/1
www.yourdomain.com/group/3
Please try below code in .htaccess
RewriteEngine On
RewriteRule ^user/([0-9]+)/$ /index.php?page=user&id=$2 [L,QSA]
RewriteRule ^group/([0-9]+)/$ /index.php?page=group&id=$2 [L,QSA]

mod_rewrite Multiple Variables and Subfolders

On my site I have multiple URLs like this:
Main Page:
mysite.com
mysite.com/?content=about
mysite.com/?content=posts&page=2
Subfolders:
mysite.com/subsite/
mysite.com/subsite/?content=about
mysite.com/subsite2/?content=posts&page=2
I'd like to make clean these up to be:
mysite.com/about/
mysite.com/posts/2
mysite.com/subsite/about/
mysite.com/subsite/posts/2
Now, I've been able to use mod_rewrite for one variable, and some other simple things, but I'm not exactly sure how to accomplish this. When I use:
RewriteRule ^([a-z]+)/([a-z0-9-]+)/?$ /?content=$1&page=$2 [L]
It recognizes the sections of the URL as variables, but it also sees the subsite as a variable, and attempts to plug in 'subsite' for 'content'.
Would anyone be able to point me in the right direction?
You could tweak you existing rule to allow for an OPTIONAL subsite/ or subsite2/ prefix e.g.
RewriteRule ^(subsite/|subsite2/)?([a-z]+)/([a-z0-9-]+)/$ /$1?content=$2&page=$3 [L]
Or just add a rule to handle the subsites before the existing rule e.g.
RewriteRule ^(subsite/|subsite2/)([a-z]+)/([a-z0-9-]+)/$ /$1?content=$2&page=$3 [L]
RewriteRule ^([a-z]+)/([a-z0-9-]+)/?$ /?content=$1&page=$2 [L]
I ended up getting this to work by putting another .htaccess file in the subsite folder.
The root .htaccess has:
RewriteEngine On
RewriteRule ^([A-Za-z0-9]+)/?$ ?a=b&content=$1 [L]
RewriteRule ^([A-Za-z0-9]+)/([A-Za-z0-9]+)/?$ ?a=b&content=$1&page=$2 [L]
In the subsite .htaccess I have: (same as root .htaccess)
RewriteEngine On
RewriteRule ^([A-Za-z0-9]+)/?$ ?a=b&content=$1 [L]
RewriteRule ^([A-Za-z0-9]+)/([A-Za-z0-9]+)/?$ ?a=b&content=$1&page=$2 [L]
If you do not need to alter the subdirectory's url, add only RewriteEngine On to the subsite's .htaccess. This basically overrides the root .htaccess rewrite rules, and loads the page from the subfolder.

How to use rewriterule to redirect to a php file in the same folder?

I would like to take requests for /somefolder/style.css and handle them with /somefolder/program.php
So, I put the following in my .htaccess file:
rewriteengine on
rewriterule ^style.css$ program.php?css=1 [R=302,L]
The result is that instead of redirecting to /somefolder/program.php, the server tries to redirect to:
/var/www/html/somefolder/program.php?css=1
How can I get rid of the /var/www/html/ in the redirect? I thought that since I just entered program.php in the .htaccess that it would default to the same folder.
Since this is a generic script that I will use in many places, I would like to avoid using rewritebase to specify the folder I'm in -- the .htaccess has to work in any folder without being modified.
Leave the R flag away and you will get an internal redirect:
RewriteRule ^style\.css$ program.php?css=1 [L]
Otherwise specify the full URL path you want to redirect to externally:
RewriteRule ^style\.css$ /program.php?css=1 [R=302,L]
Or for any arbitrary folder:
RewriteCond %{REQUEST_URI} (.*)/style\.css$
RewriteRule ^style\.css$ %1/program.php?css=1 [R=302,L]
I think the problem is that you are missing a ReWrite base statement.
Also the I would put the .htaccess file in the root directory of your site. That way you don't have to copy an .htacess file into every new directory you create. What if you want to change the name of your php file? You'd have to change every .htaccess file.
This is how I would redirect www.mydomain.com/orange/style.css to www.mydomain.com/orange/program.php?css=1 using generic rules:
RewriteEngine On
RewriteBase /
RewriteRule ^(.*)/style\.css$ $1/program.php?css=1 [L]

Categories