htaccess Default Query String Value - .htaccess

For some reason my .htaccess is adding default value for _url key.
My .htaccess
AddDefaultCharset UTF-8
DirectoryIndex index.php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?_url=/$1 [QSA,L]
</IfModule>
When I visit my project on http://localhost/my-project and dump the query string var_dump($_GET); I get following result;
array(1) {
["_url"]=>
string(11) "/index.html"
}
When I visit my project on http://localhost/my-project/test and dump the query string, I get following result;
array(1) {
["_url"]=>
string(11) "/test"
}
I'm guessing it has something to do with my .htaccess and / or server (apache) configuration.
Is there any work around for this issue? Why don't I have expected result;
array(1) {
["_url"]=>
string(11) "/"
}
Where does this index.html comes from and how do I get rid off it?

That index.html is coming due to your DirectoryIndex directive which is probably set to:
DirectoryIndex index.html index.php
When you visit http://localhost/my-project Apache loads http://localhost/my-project/index.html due to this directive and your REQUEST_URI becomes index.html (in current directory).
When RewriteRule runs it just passes current URI as GET parameter hence you get index.html in _url GET parameter.
You can tweak your rules like this:
DirectoryIndex index.php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php?_url=/$1 [QSA,L]
</IfModule>
Now this rewrite rule won't be invoked for http://localhost/my-project since it is matching .+ pattern.

Related

.htaccess Rewrite not working while original URL does

On my website I only use the public_html to serve static files, and the index.php file acts as a sort of controller, loading files from another directory to make the structure a bit nicer.
My only issue is that the URLs wont function properly when rewritten.
The original url works, but the rewritten one doesn't because of the /.
Original URL : /index.php?page=auth/login
Rewritten URL : /auth/login
Only the original one works, this is my .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
<IfModule mod_negotiation.c>
Options -Indexes
</IfModule>
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ index.php?page=$1 [QSA,L]
</IfModule>
Your regex specifically excludes paths with / in them. Try changing it to something very simple like
RewriteRule ^(.*?)/?$ index.php?page=$1 [QSA,L]
This will match anything, excluding a trailing slash

.htaccess Rewrite Rules online and localhost

On this website and on wamp localhost
http://www.aproapetot.ro/page.php?categ_id=1&id=1
http://localhost/aproapetot-backup/page.php?categ_id=1&id=1
I tried to build the .htaccess, but without any success.
This is what I wrote for online website:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^page-([0-9]+).html$ page.php?categ_id=$1
RewriteRule ^page\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
</IfModule>
and this for localhost
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /aproapetot-backup/
RewriteRule index.php index.html
RewriteRule ^page-([0-9]+).html$ page.php?categ_id=$1
RewriteRule ^page\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
</IfModule>
Someone please give a little help.
It's a website with jokes so after .ro/page.php?categ_id=1&id=1 I have a category(1) and subcategory(1).
I want to have like this
.ro/jokes/doctors instead of how is now.
I'm not entirely sure what you want based upon the htaccess content, but what I do understand is that you want /jokes/doctors is rewritten to /pages.php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !pages.php [NC]
RewriteRule ^(.*)$ /pages.php [L]
The above code will rewrite all requests to pages.php, as long as:
%{REQUEST_FILENAME} "!-f": The file itself does not exist on the server.
%{REQUEST_FILENAME} !pages.php: The request is not made to pages.php (meaning that it already has been rewritten)
Finally you can use PHP to obtain the parameters using:
/* Get all parameters from the URL */
$_GET['path'] = rawurldecode( $_SERVER['REQUEST_URI'] );
/* Remove double slashes and backslahses and convert all slashes and backslashes to '/' */
$_GET['path'] = preg_replace( array('#[/\\\\]+#', '/(\/\.)+(\/)/'), '/', $_GET['path']);
$_GET['path'][0] should contain "jokes"
$_GET['path'][0] should contain "doctors"
==edit==
Redirecting using htaccess (not the best method)
Redirect 301 /page.php?categ_id=6&id=0 /anecdotes
Redirect 301 /page.php?categ_id=1&id=1 /jokes/doctors
PHP (or other server side script). You can perform a check if the url is in fact having numbers (see $_SERVER['REQUEST_URI']) and ifso, add some header redirection to your page.
<?PHP
header('Location: www.aproapetot.ro/' . $cat . '/' . $subcat );
?>

Silex routes, htaccess

I am using htaccess like this,
RewriteEngine On
RewriteBase /link/application/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,L]
with simple index.php in /application/ folder like this
<?php
require_once __DIR__ . '/../framework/autoload.php';
$app = new Silex\Application();
$app['debug'] = true;
$app->get('/', function() {
return 'Hello!';
});
$app->run();
and when i am trying to access localhost/link, Silex gives me an error
NotFoundHttpException: No route found for "GET /link/"
so to fix it i need to change my $app->get to
$app->get('/link/', function() {
is there any methods to use RewriteEngine but not to rewrite Silex route (leave it as "/") ?
What am i doing wrong?
If Silex is placed in a subdirectory the additional .htaccess RewriteCond for directories is needed, try this:
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteBase /link/application
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [QSA,L]
</IfModule>
if your server is running Apache >= 2.2.16 you can use the FallbackResource:
FallbackResource /kit2/bootstrap.php
instead of the above code in your .htaccess.

CodeIgniter Mod_Rewrite issues

Having issues removing index.php? from the URL:
http://localhost/index.php?/reset_password
With htaccess file:
# Customized error messages.
ErrorDocument 404 /index.php
# Set the default handler.
DirectoryIndex index.php
# Various rewrite rules.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
</IfModule>
CI Settings:
$config['uri_protocol'] = 'AUTO';
I looked at the other threads on here with similar problems but none of their htaccess files work. Am I missing a setting?
I've never seen that .htaccess setup before, although it may work. I've used this for every CI project I've done, give it a try:
<IfModule mod_rewrite.c>
RewriteEngine on
# If the start of the URL doesn't match one of these...
RewriteCond $1 !^(index\.php|robots\.txt|assets|cache|themes|uploads)
# Route through index.php
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
Add don't forget this in config.php:
/*
|--------------------------------------------------------------------------
| Index File
|--------------------------------------------------------------------------
|
| Typically this will be your index.php file, unless you've renamed it to
| something else. If you are using mod_rewrite to remove the page set this
| variable so that it is blank.
|
*/
$config['index_page'] = '';
And remember: This will not "remove" index.php from your URL automatically, but rather route the request through it if it isn't present. If you have hardcoded links with index.php in them, they'll have to be changed or you'll need additional .htaccess configuration. If you are using the CI url functions like base_url(), site_url(), and anchor(), they won't use index.php if you have it blank in your config above.
This code works for me:
If name of my site is codeIgniter_ci_intro
I used code in .htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /codeIgniter_ci_intro//index.php/$1 [L]
and I changed in config.php
$config['index_page'] = '';

is if else conditions available on htaccess?

I have a .htaccess file in the folder "services" in my website
and have to give two urls following
content of htaccess
#------------------------
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^(.*)$ index.php?n=$1 [L,QSA]
</IfModule>
$-------------------------
1. http://www.mysite.com/services/seo-work
2. http://www.mysite.com/services/online-editor
The 1st URL rewritten using htaccess like http://www.mysite.com/services/index.php?s=seo-work
This is working correct. but when i am uploading other file online-editor.php on the same folder "services", it is not working
so how the first url should show the page using index.php?s=seo-work if the page "online-editor.php" is not found in directory otherwise online-editor.php shoud show. I am programatically representing my problem below
if(IsFoundPageInDirectory(online-editor.php))
{
GoToUrl(http://www.mysite.com/services/online-editor.php)
}
else
{
GoToUrl(http://www.mysite.com/services/index.php?s=seo-work)
}
Please help me to get out of this problem
Advance thanks for your reply
There is not really if/else conditions with ifModule in Apache configuration, but you can use the test in 2 cases : !module & module, like this:
<IfModule !module>
# default directives
</IfModule>
<IfModule module>
# decorator directives
</IfModule>
Check default wordpress .htaccess file. I think you're missing checks for existing files and directories. Something like
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
</IfModule>
Last I found answer.. thanks for the fist reply to lead me to get answer
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^/]+)/$ $1.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?n=$1 [L,QSA]
</IfModule>

Resources