.htaccess not redirecting properly - php

I've gone through stackoverflow.com quiet a bit but I believe I didn't find what I was looking for. So, Here it goes. I have a .htaccess file which redirects properly but I have a remote test server, which it seems doesn't redirect as my developer machine does, could that might be the server's apache2 handler isn't configured properly?
Okay, the code is as below.
RewriteRule ^welcome?(.*) public/index.php [L]
RewriteRule ^profile?(.*) public/index.php [L]
RewriteRule ^password?(.*) public/index.php [L]
RewriteRule ^verify(.*) public/index.php [L]
RewriteRule ^(.*)$ main.php?%{QUERY_STRING} [L]
Now, the main.php file handles all the request that comes in, even welcome,profile,password,verify. but Now, I want to redirect specific files only to the public/index.php file, which it does in my developer machine but remote server ?

Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteRule ^(welcome|profile|password|verify) public/index.php [NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/(welcome|profile|password|verify) [NC]
RewriteRule ^(.*)$ main.php [QSA,L]
Give this a try.

Related

Weird problem in .htacess occurring after server change

I have a site with 3 scripts, namely viewgames.php, play.php and users.php present at the root directory. I also have a .htaccess file in the same (root) directory.
My .htaccess code that cleans URLs is :-
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^search/(.*)$ ./viewgames.php?search=$1
RewriteRule ^category/(.*)$ ./viewgames.php?cat=$1
RewriteRule ^users/(.*)$ ./users.php?action=$1
RewriteRule ^play/(.*)$ ./play.php?gn=$1
RewriteRule ^([a-z]+)\/?$ $1.php [NC]
ErrorDocument 404 /pagenotfound.php
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>
When I was developing in localhost (XAMPP), I got no problem in accessing the clean URLs (using the above htaccess code) such as localhost/MySite/play/foo, localhost/MySite/category/action, localhost/MySite/search/lolol or localhost/MySite/users/myaccount.
It clearly means that the above .htaccess code was working like a charm in my localhost server. Then I uploaded the above code to a shared Apache-based linux server, and encountered some weird problems -
https://example.com/category/blabla was working fine, and so was https://example.com/search/blabla
But the URLs https://example.com/play/xyz and https://example.com/users/anyaction stopped working. By that I mean, the page was loading fine, the site was there, but no $_GET values were passed to the PHP script. Thus, I am not able to get the value of parametes gn and action. (see above code)
I even tried by var_dump-ing the $_GET array in the scripts, and they returned an empty array, meaning that no values were sent to the script.
Please check my .htaccess code. I am completely and utterly new to .htaccess, and the above code is merely a copied one. I will really appreciate any help in the matter.
Have it like this:
ErrorDocument 404 /pagenotfound.php
Options -MultiViews
RewriteEngine on
RewriteBase /
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
RewriteRule ^search/(.*)$ viewgames.php?search=$1 [L,QSA,NC]
RewriteRule ^category/(.*)$ viewgames.php?cat=$1 [L,QSA,NC]
RewriteRule ^users/(.*)$ users.php?action=$1 [L,QSA,NC]
RewriteRule ^play/(.*)$ play.php?gn=$1 [L,QSA,NC]
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^([a-z]+)/?$ $1.php [L,NC]
It is important to keep MultiViews options off to avoid content negotiation feature causing issues for mod_rewrite module.

Run a Zend1 app on localhost

I need to run a zend1 app on my local system which is working on a production server successfully.
The problem I'm facing currently is that when I run it through localhost/example it shows me nothing, just a blank page and when I try to call a controller directly like localhost/example/controller1 it moves to 404.
I've searched the issue on Google and tried to implement all the possible solutions, but no luck.
Here is the .htaccess of the app
RewriteEngine on
RewriteCond %{HTTP_REFERER} semalt\.com [NC]
RewriteRule .* - [F]
RewriteBase /
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^media\/\.cache\/(.*)$ image.php?file=$1 [L]
RewriteRule ^([a-zA-Z0-9\_\/\\\-]+)?$ index.php [L]
You don't need to write to much things in .htaccess.
you can use this few line code for .htaccess file.
and you need to put this .htaccess file and index.php file on root folder.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
Options -Indexes

Making .htaccess rewrite all requests to /web with a front controller?

I'm trying to route all requests to /web/index.php (front controller) while serving static files directly if they exist.
The following .htaccess file was taken from a Symfony 2 application, and seems to work fine as it is. Requests are sent to ./index.php and I can access to static files like ./web/css/style.css. I'm working on a shared hosting (not a good one) and this .htaccess seems the only one working (this will cause a 500 error).
How can I have route all requests to /web folder? It seems that I need to change something in E=BASE variable, I've tried [E=BASE:%1/web] but it doesn't work.
I have a little understanding of rewrite rules, can you point me to the right direction?
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
RewriteRule ^(.*) - [E=BASE:%1]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^index\.php(/(.*)|$) %{ENV:BASE}/$2 [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule .? - [L]
# Rewrite all other queries to the front controller.
RewriteRule .? %{ENV:BASE}/index.php [L]
</IfModule>
I may be way off here... but isn't it just: change the last line to:
RewriteRule .? %{ENV:BASE}/web/index.php [L]
For the static files it needs to be looking in your /web folder to check if the file exists. Currently the RewriteCond %{REQUEST_FILENAME} -f just checks if the filepath requested exists.
Try throwing a RewriteBase command in before the RewriteCond %{REQUEST_FILENAME} -f:
RewriteBase /web/
RewriteCond %{REQUEST_FILENAME} -f
You can replace your .htaccess with this code:
RewriteEngine On
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^(.*?)index\.php$ /$1 [L,R=302,NC,NE]
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^((?!web/).*)$ web/$1 [L,NC]
Let's assume that your domain is gremo_silex.com, and your hosting has directory for it like
~/domains/gremo_silex.com/public_html
(all hosting services I used last few years had directory structure similar to this).
Then you can place your Silex project one directory up (in ~/domains/gremo_silex.com) and rename your web directory to public_html, this way there's no web in your url's.
I encountered the same issue today. Normally the web host would allow you to change Apache's document root (my preferred method) but in this case it wasn't possible. So with some hackery I compiled this:
<IfModule mod_rewrite.c>
RewriteEngine on
# Serve all public files from /web/
RewriteCond %{REQUEST_URI} !web/
RewriteRule (.*) /web/$1 [L]
# If file doesn't exist send to front controller
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php [QSA,L]
</IfModule>
The REQUEST_URI header comes in to PHP without the /web prefix too, so routing should work out of the box.

CodeIgniter can't remove /index.php/ from url [duplicate]

I have done this lots of time. But than also I am stuck here again (in a different server) and can not figure out what is the issue.
Completed htaccess editing
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /glt.me/CI/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /glt.me/CI/index.php/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 /index.php
</IfModule>
rewrite module is enabled (as per customer care perons says)
Error :
Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.
Please contact the server administrator, cgiadmin#yourhostingaccount.com and inform them of the time the error occurred, and anything you might have done that may have caused the error.
More information about this error may be available in the server error log.
Additionally, a 500 Internal Server Error error was encountered while trying to use an ErrorDocument to handle the request.
Test this case :
http://glt.me/CI/test
http://glt.me/CI/index.php/test
This is the simple way and it works fine for removing index.php from your url
RewriteEngine on
RewriteCond $1 !^(index\.php|uploads|css|js|lib|img|bootstrap|robots\.txt)
RewriteRule ^(.*)$ /manage/index.php/$1 [L]
instead of manage put your own application folder name.
Use following htaccess code to remove index.php from your codeigniter url
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt|css)
RewriteRule ^(.*)$ ./index.php/$1 [L]
Though your client confirmed the rewrite module is working, what don't you confirm it yourself, make a php file on the root of the project with the code below and try to browse the file.
<?php
if( !function_exists('apache_get_modules')){
echo 'Rewrite module not available';
}else{
if(in_array('mod_rewrite',apache_get_modules()))
echo 'Rewrite module enabled';
}
First check whether your apache is supporting mod_rewrite. If yes then add a .htaccess file. My codeigniter .htaccess file is:
DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|images|css|js|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php/$1 [L,QSA]
Finally in your config file, change your base_url, remove index.php from that if you have added.
DirectoryIndex index.php
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php/$1 [L,QSA]
RewriteRule ^pages/.*$ http://localhost/test/? [R=301,L]
I wanted to contribute here as I was having trouble myself. I'm using IIS5.1 for development only (Highly not recommended!)
1- Make sure that you config.php file has:
$config['index_page'] = '';
2- My app is not on the root folder, it's at localhost/CodeIgniter/. I updated config.php to:
$config['base_url'] = 'http://localhost/CodeIgniter/';
3- You need Mod-Rewrite capabilities. This is embedded with most servers, but IIS5.1 needs a separate tool. I used: IIS Mod-Rewrite by micronovae. It's free as long as you use it on localhost
I put the following code (Instead of CodeIgniter, put the name of the folder):
RewriteEngine on
RewriteCond $0 !^/CodeIgniter/(css|js|swf|images|system|tools|themes|index\.php) [NC]
RewriteRule ^/CodeIgniter/(.*)$ /CodeIgniter/index.php/$1 [L]
If your application is at the root, the code you should use would be simply:
RewriteEngine on
RewriteCond $0 !^(css|js|swf|images|system|tools|themes|index\.php) [NC]
RewriteRule ^(.*)$ index.php/$1 [L]
Hope it helps.
Only three steps are require to remove index.php from url in Codeigniter in WAMP environment.
1) Create .htacess file in parallel to application holder and just copy past the following code:
RewriteEngine On
RewriteBase /CodeIgniter/
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
2) Change $config['index_page'] to blank in config.php in application folder as below:
$config['index_page'] = '';
3) Enable “rewrite_module” of apache.
Restart your apache and its done.
Details : http://sforsuresh.in/removing-index-php-from-url-of-codeigniter-in-wamp/

how to redirect routing with .htacces

My problem is i bought a shity hosting and now i can not set my own config.
I need to do it by .htaccess file.
Here is what i manage to do:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?domain.com$
RewriteCond %{REQUEST_URI} !^/Project/web/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /Project/web/$1
RewriteCond %{HTTP_HOST} ^(www.)?domain.com$
RewriteRule ^(/)?$ Project/web/app.php [L]
I know that this will not work with some website software like mine. and I also need to modify the $base_url, $live_site or other configuration settings in those to finish the process.
To make it all clear with this settings when i enter my domain i start from subdirectory i choose. but when i try to go to another page its return 404 error.
I have an idea how to make it work, its need to redirect also routing
so for example www.domain.com/page need to be redirected to www.domain.com/Project/web/page if anyone know how to do it ?
I installed symfony on a shared hosting service one time.
That's what I had to do to make Symfony work :
in the public directory there is the structure of the project :
/app
/src
/vendor
/web/.htaccess
/.htaccess
In /.htaccess I wrote :
SetEnv PHP_VER 5_3
SetEnv REGISTER_GLOBALS 0
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /web
RewriteCond %{REQUEST_URI} \.(css|gif|ico|jpg|js|png|swf|txt|pdf|doc|docx|mp3|svg)$
RewriteRule ^(.*)$ $1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ web/app.php/$1 [QSA,L]
</IfModule>
in /web/.htaccess I wrote :
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ app.php [QSA,L]
</IfModule>
That's all...
Maybe the PHP header function?
<?php header("Location: http://www.example.com/"); exit; ?>
PHP: header

Categories