htaccess Rewrite to same name file in different folders of site - apache

I tried to search a lot but got no success. I wish that someone could really help me out with this.
Ok, so my website is like:
/rewrite.php
/folder1/rewrite.php
/folder2/rewrite.php
/folder1/subfolder1/rewrite.php
/folder1/subfolder2/rewrite.php
and so on...
In short, there exist a rewrite.php in each and every folder(except admin access folder).
So basically, I want to rewrite to the rewrite.php file in the corresponding directory, but I am not able to do it.
That is
If I request
/News
then it should rewrite it to:
/rewrite.php
If I request:
/folder1/News
then it should rewrite it to:
/folder1/rewrite.php
and so on for other folders.
Currently, this is what I have in my htaccess:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . rewrite.php [L,NC]
Somewhere, I read that If I remove RewriteBase, then it uses the current directory as RewriteBase. But it failed and didn't worked.
Also, I would like to keep some folders in exceptions(in which Rewriting should be disabled) like:
/administration
/config
I hope for a feasible solution, if anyone can help me... :)
Best Regards,
Ankur Thakur

Try:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/(administration|config|rewrite.php)
RewriteRule ^(.*?)(/?[^/]+)$ $1/rewrite.php [L]

Related

RewriteRule doesn't rewrite correctly

I'm hosting my development site on the localhost server and I used to access it at 127.0.0.1/dev. In the folder I have a .htaccess file containing following information. I'm new with the RewriteEngine and don't get this work.
RewriteEngine on
RewriteRule ^/(.*)$ /index.php?page=$1
When I'm trying to access 127.0.0.1/dev/home, I simply get a message that the page doesn't found. I can't see where rewrite is redirecting me, so I can't debug the problem in easy way. I think that you can see the problem at the first look.
Thanks in advance.
Try this rule in /dev/.htaccess without leading slash in source pattern and target URL:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php?page=$0 [L,QSA]

.htaccess affect other file url

I am trying to make .htaccess rule not affect other file url
example
my .htaccess rule is
RewriteEngine On
RewriteRule ^([^/]*)$ /tr/hp.php?q=$1 [L]
my site url is
mydomain.com/keywords
everything working good on keywords but when I try to open robots.txt
mydomain.com/robots.txt
OR
mydomain.com/images.jpg
any other file url
redirect on /tr/hp.php?q=filename
which .htaccess Rewrite Rule works on both?
Try :
RewriteEngine On
#--exclude real directories
RewriteCond %{REQUEST_FILENAME} !-d
#--and files
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ /tr/hp.php?q=$1 [L]
You also have to prevent matching any request pattern that carries a dot in it:
RewriteEngine On
RewriteRule ^([^/.]*)$ /tr/hp.php?q=$1 [L]
Certainly it is possible to further refine this pattern.
Alternatively some people like to prevent rewriting requests to files or folders that physically exist in the file system using RewriteCond:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
But that is something you have to decide upon. This does not help for example if resources like robots.txt are delivered in a dynamic manner...

.htaccess - Rewrite subfolder to parent with access to other files in the parent

I am trying to create a RewriteRule which would allow me to hide the subfolder name from the URL. It seems pretty straightforward, although I would also like to retain access to the files in the parent folder, and I can't work out how to achieve this and whether this would be possible at all. To make it a bit more clear, my file tree is as follows:
[root]
L[parent]
file1
L[subfolder]
file2
So what I am trying to do, is to be able to rewrite http://example.com/parent/subfolder/file2 to http://example.com/parent/file2 while still having access to http://example.com/parent/file1. So far I found possible solutions here and here, but neither of them work as expected. I also had a look at the Apache Aliases, but I am not sure I can use them in this case. Any suggestions please?
Edit: Here is the current .htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /pico/
RewriteRule sitemap\.xml \?sitemap
RewriteRule ^parent?$ parent/ [R=301,L] ## Redirects the old file to the index with the same name
RewriteRule ^parent/subfolder/?$ parent/ [R=301,L] ## Redirects the index of the subfolder to the parent
RewriteRule ^old-folder/?$ parent/subfolder/new-file1 [R=301,L] ## Redirects the old index to a new file in the subfolder
RewriteRule ^old-folder/old-file parent/new-file [R=301,L] ## Redirects the files in the old folder to a new file in the parent
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
RewriteRule ^parent/([^/]+)/?$ parent/subfolder/$1 [L]
</IfModule>
# Prevent file browsing
Options -Indexes
The rules with the comments are just examples, since I am moving the old content to the new site while changing the structure, so there are many more repeating rules just like the examples above.
Did you just try this? Put this in an .htaccess file in the root.
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^parent/([^/]+)/?$ /parent/subfolder/$1 [L]
Edit:
Based on your comment I'm explaining what I put above. You said you wanted anything that is in the parent/subfolder to be read as if it is in the parent location directly. So that means http://example.com/parent/file2 does NOT really exist. Because it actually exists in http://example.com/parent/subfolder/file2.
That is the purpose of the conditions. It checks to make sure that any request for /parent/whatever is NOT a real file and NOT a real directory, and if that is true it rewrites it to /parent/subfolder/whatever. So it is what you asked for but you're not using the rules the way I gave them to you. All you did was add the rewriterule line to the bottom of your file and it will never execute because this rule
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
will execute before RewriteRule ^parent/([^/]+)/?$ /parent/subfolder/$1 [L] and then it will never run. You can't just provide pieces of your .htaccess rules when asking a question because some rules will take precedent over others depending on their order. You need to provide all your rules as you just now did.
So you need to change your rules to this.
RewriteRule sitemap\.xml \?sitemap
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^parent/([^/]+)/?$ /parent/subfolder/$1 [L]
RewriteRule ^parent?$ parent/ [R=301,L] ## Redirects the old file to the index with the same name
RewriteRule ^parent/subfolder/?$ parent/ [R=301,L] ## Redirects the index of the subfolder to the parent
RewriteRule ^old-folder/?$ parent/subfolder/new-file1 [R=301,L] ## Redirects the old index to a new file in the subfolder
RewriteRule ^old-folder/old-file parent/new-file [R=301,L] ## Redirects the files in the old folder to a new file in the parent
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]

How to make URL rewrite rule in subdirectory

I have a subdirectory query in my root domain, i.e., domain.com/query/.
Now I want to make a url rewrite rule by changing .htaccess file. Here is the file I made:
RewriteEngine on
RewriteRule ^query/(.*)$ query/index.php?action=$1 [NC]
But it is not working at all. I'm doing this on 1and1 performance hosting.
What could possibly be wrong? This is a big headache for me, and I have read some other posts on Stack Overflow. Thanks so much for help!!
You need to use RewriteBase and tweak your rule accordingly:
RewriteEngine on
RewriteBase /query/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?action=$1 [L,QSA]

.htaccess on specific filetypes

I have some magic stuff in my htaccess. It works very well but I don't know how
RewriteRule ^[^_+/]. index.php
I think it says that all requests should go through index.php no matter which directory the visitor ask for. So far so good. However images, css files, js files etc should of course not be parsed in index.php. Can I exclude certain directories from the above rule? Like /img, /css and /js
Other suggestions?
Thanks in advance
you can exclude existing files and directories
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
by placing this before your rule
You can avoid requests to certain paths by using a simple RewriteCond...
RewriteCond ${REQUEST_URI} !^css
RewriteCond %{REQUEST_URI} !^img
RewriteCond %{REQUEST_URI} !^js
# or alternatively just
RewriteCond ${REQUEST_URI} !^(css|img|js)