.htaccess in subfolder doesn't work - apache

I'm hosting my site on Godaddy server.
On the server I have 5 sites, each site on individual folder.
for example: "webroot/site1"
I directed specific domain to the specific folder on the server "/site1"
The problem:
I tried to make a RewriteRule with .htaccess in the subfolder but it keeps telling me 404 not found.
This is the .htaccess:
RewriteEngine On
this works fine:
RewriteCond %{HTTP_HOST} ^example.com
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301]
this redirect to 404 page:
#rewirite categories only
RewriteRule ^category/([0-9]+)/.*$ /category.php?c_id=$1 [L,QSA]
this url works perfect:
www.example.com/category.php?id=1
this url doesn't work and redirect to 404 page
www.example.com/category/1/blalba
More details:
The other 4 folders are word press sites.
In the webroot there is another wordpress site.
In the webroot there is an empty htaccess file.
I already tried to do
I already tried to do:
RewriteRule ^category/([0-9]+)/.*$ /site1/category.php?c_id=$1 [L,QSA]
EDIT:
I don't have category folder is just for the URL I want that it will redirect me to category.php with the id parameter
This is the error I get:
Not Found
The requested URL /site1/category/1/area-rugs was not found on this server.
Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.

The RewriteRule will need to be in the htaccess file in the root folder of the website.
E.g. if the directory structure is something like this:
/webroot/site1.com/
/webroot/site1.com/.htaccess
/webroot/site1.com/index.php
/webroot/site1.com/category.php
I'd have this code in the .htaccess file in site1.com:
RewriteEngine On
# Force www
RewriteCond %{HTTP_HOST} ^site1.com
RewriteRule ^(.*)$ http://www.site1.com/$1 [R=301]
# Handle categories
RewriteRule ^category/([0-9]+) /category.php?c_id=$1 [L,QSA]

Related

How can redirect all my site links to another domain except "index.php" and some other files

How can I redirect all the requests to my site to another domain except the file /index.php and the file of /members/ and /Admins/.
At the top of your .htaccess try the following using mod_rewrite:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/(members|Admins)/
RewriteRule !^(index\.php)?$ https://other.example%{REQUEST_URI} [R,L]
This assumes that your "other domain" (eg. other.example) points to a different server.

redirectMatch in mod rewrite

We recently moved our web site to another server, say http://newdomain.com, while our old site is at say http://olddomain.com
I would like to keep the users directory on the old site accessible as it was before, that is http://oldomain.com/~user, but anything accessible under the root folder point to the new domain. The users directory on the old server are in the /home/user file system. So, I wrote following rules:
RedirectMatch permanent ^/~(.*) /home/~$1
RedirectMatch permanent ^/(.*) http://newdomain.com/$1
while the second rule works flawlessly, the first one still wants to map to the new site as following: http://newdomain.com/~user.
How can I fix the two rules so that anything in /var/www/html on the old site redirects to the new site, but anything under /home does not redirect?
--
Here is the new code based on Ben's solution, which maps the urls for the access to users' home pages correctly, but the browser complains and does not show their sites. The root folder urls redirection works fine.
RewriteEngine on
# RewriteBase / -- I had to comment this as the apache did not like it
Rewriterule ^/~(.*) /~$1 [R=301,L]
RewriteCond %{REQUEST_URI} !^/home/~
RewriteRule ^/(.*) http://newdomain.com/$1 [R=301,L]
You should use RewriteCond to test if the URI prefix with /home/~, and use RewriteRule to rewrite the URL and make a permanent redirect.
RewriteEngine On
RewriteBase /
RewriteRule ^~(.*) /home/~$1 [R=301,L]
RewriteCond %{REQUEST_URI} !^/home/~
RewriteRule ^(.*) http://newdomain.com/$1 [R=301,L]
Line 3: if URI prefix with ~{any_characters}, rewrite the URI to /home/~{any_characters}, redirect permanently
Line 5: Test if the URI not prefix with /home/~{any_characters}
Line 6: Then process the rewrite rule, rewrite the URI from {any_characters} to http://newdomain.com/{any_characters}, redirect permanently

HTAccess: Internal redirect directories (or symlink?)

I'm currently struggelling with the following situation which I want to solve with HTacess redirection.
The main CMS is running in the root directory /.
In the subdirectory /shop/.. there is a shop (based on another system)
Till now everything is fine: The main website is rewritten by /.htaccess and the shop by /shop/.htaccess
Now I want to access an english version of the shop with /en/shop/..
Actually the calls should only be redirected to the shop-system in the directory /shop (but with the url /en/shop/.. in the browser)
I tried the following:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/en/shop/$
RewriteRule ^(.*)$ /shop/$1 [L,QSA]
Which is not really working.
My question is: Can I solve that with simple htaccess or would it be a more elegant solution to make a symlink /en/shop => /shop ?
(Are there any performance differences between these solutions?)
Thanks in advance
Put the following code at the root folder .htaccess file :
RewriteEngine on
RewriteBase /
RewriteCond %{THE_REQUEST} !\s/+en/ [NC]
RewriteRule ^shop/ en%{REQUEST_URI} [R=302,L,NE]
RewriteRule ^en/(.*)$ $1 [L,NC]

Page not found when rewriting URL through .htaccess

I've been having problems trying to set up my .htaccess for the site.
I am trying to remove a folder from the URL when a page accesses this. I managed to successfully remove the folder from the path using this post How can I remove this directory/folder from url with htaccess rewrite? but when I load the page up, none of the css, javascript or other resources are loaded correctly.
When I try to access the resource from the url it says 'The requested URL /modules/content/styles/default.css was not found on this server.'
Modules is the folder I am trying to remove when accessing a page, however it seems that when I added the code to htaccess, it's adding "modules" to all paths.
My file structure looks like this
I have a .htaccess file in the root directory with the code:
RewriteEngine on
RewriteRule !^/?modules modules%{REQUEST_URI} [L,NC]
I also have one in the modules folder with the code:
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+modules([^\s]*) [NC]
RewriteRule ^ %1 [R=301,L]
I have tried multiple solutions but all end with "Page not found", this is the closest I have gotten but now I am having a problem with resources.
Any help would be great!
You need to change this rule in root .htaccess:
RewriteEngine on
# If the request is not for a valid directory
RewriteCond %{REQUEST_FILENAME} !-d
# If the request is not for a valid file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule !^modules/ modules%{REQUEST_URI} [L,NC]

Apache htaccess sending URLs in a URL

I'm trying to send a URL in a URL, and there's ModRewrite in the middle.
Here's the Rewrite Rule. It's used to search for a user's profile.
RewriteEngine On
RewriteRule ^(query)/(profile)/([a-z0-9:.()\-\ ]+)/?$ search/index.php?scope=$2&query=$3 [NC,L]
And this is the URL
http://www.example.com/query/profile/http://www.facebook.com/example
No matter how I do the rewrite rule, I keep getting NetworkError: 500 Internal Server Error or NetworkError: 404 Not Found if it's a URL I put there.
How can I do this right? I'm on Windows 7 Home Premium. Am I missing something?
The URL above is passed using ajax.
Edit:
#htaccess in site1 folder
RewriteRule ^resources/?(.*)/?$ /control/$1 [NC,L]
#htaccess in control folder
RewriteRule ^(query)/(profile)/([a-z0-9:.()\-\ ]+)/?$ search/index.php?scope=$2&query=$3 [NC,L]
next folder is `search` which has `index.php`
URL used
http://www.example.com/resources/query/profile/http://www.facebook.com/example
EDIT:
I redid the RewriteRule like below, and don't get any error messages now, but Apache seems to be taking off one / from the url after the http://. So I get only http:/www.facebook.com/example. Any help with that will be appreciated.
RewriteRule ^(query)/(profile)/([a-z0-9\/:.()\-\ ]+)/?$ search/index.php?scope=$2&query=$3 [NC,L]`
You need to capture URL from RewriteCond otherwise Apache will trim multiple // into a single one.
Change this rule in root .htaccess:
RewriteCond %{REQUEST_URI} /resources/(.+)$ [NC]
RewriteRule ^ /control/%1 [L]
Have this in /control/.htaccess file:
RewriteEngine On
RewriteBase /control/
RewriteCond %{REQUEST_URI} /(query)/(profile)/(.+)$ [NC]
RewriteRule ^ /resources/search/index.php?scope=%2&query=%3 [QSA,L]