2nd Level .htaccess not working - php

I used to have two .htaccess files in my projet, We have the following directories.
MainProject/
.htaccess //first .htaccess
public/
.htaccess //second .htaccess
The projects used to work fine, but once I've uninstalled and installed apache, everything changed.
This is .htaccess 1 :
RewriteEngine On
RewriteRule ^$ public/ [L]
RewriteRule (.*) public/$1 [L]
And, this is .htaccess 2 :
RewriteEngine On
RewriteCond %{REQUEST_URI} !/public/* [NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [L,QSA]
I activated the mod_rewrite module.
If I write in the address bar :
http://myproject.lan/
It takes me to the home page, meaning that .htaccess 1 is working just fine.
But when I write something like :
http://myproject.lan/login
It doesn't work, and it shows a "page not found" message.
What happens when project works, is that /login = $1 , so, it looks for public/login (in .htaccess 1)
Then, in public directory, the server executes the second .htaccess, where $1 matched to /index.php?url=/login, which is eventually sent to the front controller, waiting to be processed and converted to an existing route.
If there's something I need to enable on Apache, let me know because I forgot how I managed to make it work before in the first place.
Thank you

Your /public/.htaccess should be like this:
DirectoryIndex index.php
RewriteEngine On
RewriteBase /public/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php?url=$1 [L,QSA]
Problem is presence of RewriteCond %{REQUEST_URI} !/public/* [NC] condition in this .htaccess

I just found the solution to my problem, The problem is that in my
httpd-vhosts.conf
I forgot to add /public to the DocumentRoot.
Thank you

Related

Can't remove codeigniter's index.php in a subfolder of a wordpress site on root

I'm aware of a lot of solutions around there, and tried everything. The problem is, when I put a .htaccess in the codeigniter subfolder, it's not read. I thinks it's because of the AllowOverride directive, but I can't change that.
I tried to skip the main subfolder that is not part of wordpress, one I called "sandbox", in the root htaccess (that's part of wordpress), so it doesn't "inherit" any problem from that rewrite (for example, to not have 404 managed by wordpress in the codeigniter project).
The problem is, as I said, that the .htaccess in the /sandbox/ciproject/ folder is not read. So, I tried this in the main .htaccess, and it worked... for a little time.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/?sandbox/
RewriteRule . /index.php [L]
RewriteBase /sandbox/ciproject/
RewriteCond $1 ^(application|system|private|logs)
RewriteRule ^(.*)$ index.php/access_denied/$1 [PT,L]
RewriteCond $1 ^(index\.php|robots\.txt|favicon\.ico|public|assets|css|js|img|fonts)
RewriteRule ^(.*)$ - [PT,L]
RewriteRule ^(.*)$ index.php/$1 [PT,L]
</IfModule>
That allowed me to make it work for a while... but it's not working anymore. Something more I could try? I already changed the config of the codeigniter project so it doesn't use index.php, but nothing. I only get a server error 500 now, and if I delete the second half of the htaccess, I only get 404 as a response for getting rid of the index.php
I don't know if I'm losing some data, but just ask and I'll edit this, so you're not blind at this one.
Based on my understanding, your code-ignitor part doesn't work because, you have two clashing rewrite rules in your htaccess.
RewriteRule ^(.*)$ - [PT,L]
RewriteRule ^(.*)$ index.php/$1 [PT,L]
These are a clashing rule set. Remove one of the lines and then try. It should work.
Use only this code in htaccess file
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /sandbox/ciproject/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite all other URLs to index.php/URL
RewriteRule ^(.*)$ index.php?url=$1 [PT,L]

Apache Mod Rewrite For Front Controller Pattern

I have the following in my .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ _service.php?uri=$1 [NC,L]
</IfModule>
Only problem is there is no rewrite happening and I simply see a directory listing when attempting to access the root page of the server localhost.
Even when I try to make the match optional RewriteRule ^(.*)?$ _service.php?uri=$1 [NC,L] it does not work.
The rewrite seems fine for any other case e.g. localhost/foo/bar
How do I get the rewrite to happen for this particular case?
The problem is that the root is a directory. So this condition is preventing your rule from getting applied:
RewriteCond %{REQUEST_FILENAME} !-d
Try adding a rule like:
RewriteRule ^$ _service.php?uri=/ [NC,L]
or something
You might need to specify the document root in order for the file or directory to be found; my variation on this pattern is:
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}%{SCRIPT_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}%{SCRIPT_FILENAME} !-d
RewriteRule . /index.php [L,QSA,B]
The root page is served correctly (for me) in this case (e.g. http://www.example.com/). NB, I have this in vhost.conf and not in a .htaccess.

htaccess RewriteRule to shorten the URLs

I would like to find a RewriteRule that does this :
mysite.net/jqMAS/ or mysite.net/jqMAS => mysite.net/index.php?id=jqMAS
I used such a .htaccess file :
RewriteEngine On
RewriteRule ^(.*) index.php?id=$1
But unfortunately, it doesn't work (maybe mysite.net/index.php is itself redirected to mysite.net/index.php?index.php, etc. ?) : calling mysite.net/jqMAS produces a 500 Internal Server Error.
What RewriteRule should we use to do such URL shortening ?
Here is what the index.php page (I didn't mention the headers) looks like :
<body>
Bonjour <?php echo $_GET['id']; ?>
</body>
Try the following your .htaccess file, as it is the setup successfully used on my own website.
# Enable the rewriting engine.
RewriteEngine On
# Change requests for a shorter URL
# Requires one or more characters to follow the domain name to be redirected
RewriteRule ^([A-Za-z0-9-]+)/?$ index.php?id=$1 [L]
There are 2 htaccess files:
Keep your htaccess file within application folder as:
Deny from all.
and paste following code to your htaccess file outside the application folder:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
It is working perfect for me.
You need RewriteCond to stop rewriting for real files and directories:
RewriteEngine On
# if request is not for a directory
RewriteCond %{REQUEST_FILENAME} !-d
# if request is not for a file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php?id=$1 [L,QSA]

.htaccess - works only with index.php/ not with /

I have an test project in sub directory (G:/wamp/www/test) which has only index.php and .htaccess
I tried to make friendly urls by adding RewriteRule to .htaccess like this:
RewriteEngine on
DirectoryIndex index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
RewriteRule ^/*$ index.php
This worked on all other projects but it is not working on this one. When I want to access an URL like this:
http://localhost/test/TESTING_URL I am getting default 404 Not Found page. However, if I go like this: http://localhost/test/index.php/TESTING_URL - it works.
Apache error log is giving me the following error:
File does not exist: G:/wamp/www/test/TESTING_URL
The following worked for me:
RewriteEngine on
RewriteBase /test/
DirectoryIndex index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
RewriteRule ^/*$ index.php
Make sure that this .htaccess file is in the directory test and that there aren't any conflicting rewrite rules in any .htaccess file at a higher level.
Otherwise the only difference is the addition of the RewriteBase line.

.htaccess all request to other file

Currently I have the following .htaccess file
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ public/index.php/$1 [L]
</IfModule>
Which works allmost perfect.
It rewerites urls like http://domain.com/something/ to the public/index.php file, like a charm, except when it is a file, just like it should.
However http://domain.com (without any path appended) (there is no index.php in the root, so it gives a 404 at the moment) is not being rewrited, how can I change this .htaccess so it rewrites this url too?
The index file is in public/index.php I want it to load that file through the use of .htaccess
Thanks
I believe to rewrite the root, you can simply do something along the lines of:
RewriteRule ^$ location/of/root/file [L]
You could try:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ public/index.php
RewriteBase should prepend the rule pattern with a leading slash, forcing it to match the root path.
Untested!
What you have there is inspired by WordPress?? It's a bad idea as it tell Apache to always check if the path is a file or a directory before redirecting.
I have something like this
RewriteEngine On
RewriteCond %{REQUEST_URI} !^.*/(css|images|javascript)(.*) [NC]
RewriteCond %{REQUEST_URI} !\.(swf|ico|php|xml)$ [NC]
RewriteCond %{REQUEST_URI} !robots.txt
RewriteRule (.*) index.php?page=$1&%{QUERY_STRING} [PT]
The first condition restricts this redirect from working in specific folders.
The seconds does it for specific extensions.
You can guess what the third does :)

Categories