How to make URL rewrite rule in subdirectory - apache

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]

Related

Apache RewriteRule not working – page not found

I have no idea why it doesn't work, /spelling/30000 gives the Not Found page. Please help.
RewriteEngine On
RewriteBase /
RewriteRule ^/([0-9]+)$ /?mod=spelling&word=$1 [PT]
RewriteRule ^$ /?mod=spelling [PT]
With your shown samples please try following .htaccess rules file. Considering that index.php file is the one which is taking traffic in backend for internal rewrite rules. Please make sure to keep your .htaccess file along side with index.php file.
Also make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteBase /
RewriteRule ^/?$ index.php?mod=spelling [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)/?$ index.php?mod=$1&word=$2 [QSA,L]

htaccess to route all requests through index add add trailing slash

This probably seems like quite a simple question to most of you, but i'm having a real hard time getting my site to route all requests through the index file whilst also enforcing trailing slashes. I've searched high and low and not found an appropriate solution. My .htaccess so far is as follows:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [QSA,L]
RewriteCond %{REQUEST_URI} /+[^\.]+$
RewriteRule ^(.+[^/])$ %{REQUEST_URI}/ [R=301,L]
This is as far as i have managed to get and works apart from it adds the index.php into the url, for example if a user goes to;
www.example.com/about-us
I would like it to route through index and become;
www.example.com/about-us/
However my above htaccess file produces;
www.example.com/index.php/about-us/
Seems so close yet so far, anything i do to the htaccess file to try and remove the index.php just stops it from working entirely. I am aware my solution is far from perfect, it still allows access to directories and files however its the best i've been able to come up with (would ideally like to deny directory listing!)
Any help is greatly appreciated!
The ordering of rules is quite important in these cases. Switch the two rules order, and you should be getting the desired behaviour:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} /+[^.]+$
RewriteRule ^(.+[^/])$ %{REQUEST_URI}/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [QSA,L]

htaccess Rewrite to same name file in different folders of site

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]

.htaccess mod_rewrite not working correctly

I'm using Wamp on my dev machine to develop a project. This is my .htaccess:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?$1 [L]
RewriteBase /project/
This is put in localhost/project, so that any requests like localhost/project/something , are routed to localhost/project/index.php/something
However, they are being routed to localhost/index.php/something
I suspect it has something to do with how I'm using RewriteBase. How do I fix it?
You need to remove the leading slash in your rule:
# no slash---------v
RewriteRule ^(.*)$ index.php?$1 [L]
Apache kind of guesses whether the targets of a RewriteRule is a URL-path or a file-path. When it starts with a slash, it assumes it's a URL-path and that it's absolute, so it'll go to the document root's index.php. Without the slash, it'll either guess a file-path or use the rewrite base to help determine which to use.
You should also move the RewriteBase directive above your rule.
Why not put the base in the rewriterule?
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /project/index.php?$1 [L]
Edit:
This question might help you: htaccess RewriteBase

Apache ignore htaccess

I have a website that uses mod_rewrite for pretty urls. I have two main urls:
example.com/id
example.com/generate/id
And this htaccess file:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^generate/([a-z0-9_-]+)$ generate.php?id=$1 [L,NC]
RewriteRule ^([a-z0-9_-]+)$ contact.php?id=$1 [L,NC]
The first url is working correctly, but the second not. Apache shows a 404 error, I think that it's because it's looking for the folder "generate", that doesn't exist, and it can't find the htaccess in the document root.
Thank you
Have you try
RewriteRule ^generate\/([a-z0-9_-]+)$ generate.php?id=$1 [L,NC]