[mod_rewrite]: Problems with redirecting - php

i have a problem with rewriting urls. i have coded a small framework for myself because i don't need a full sized framework with lots of stuff for my projects. I defined the general behavior like zend framework:
Rewritten url: domain/controller/action
Real path: domain/index.php?ctrl=controller&act=action
Controller and action should be displayed instead of it $_GET named key. After controller and action if i add an infinite amount of additional parameters, it should be added as key/value like this:
domain/controller/action/key/value/key2/value2/key3/value3 ...
This is my .htaccess:
RewriteEngine On
RewriteBase /
RewriteRule ^([\w-]+)/([\w-]+)/([\w-]+)/([\w-]+)/([\w-]+)/([\w-]+)/([\w-]+)/([\w-]+)/([\w-]+)/([\w-]+)/([\w-]+)/([\w-]+)/([\w-]+)/([\w-]+)/?$ index.php?ctrl=$1&act=$2&$3=$4&$5=$6&$7=$8&$9=$10&$11=$12&$13=$14 [L]
RewriteRule ^([\w-]+)/([\w-]+)/([\w-]+)/([\w-]+)/([\w-]+)/([\w-]+)/([\w-]+)/([\w-]+)/([\w-]+)/([\w-]+)/([\w-]+)/([\w-]+)/?$ index.php?ctrl=$1&act=$2&$3=$4&$5=$6&$7=$8&$9=$10&$11=$12 [L]
RewriteRule ^([\w-]+)/([\w-]+)/([\w-]+)/([\w-]+)/([\w-]+)/([\w-]+)/([\w-]+)/([\w-]+)/([\w-]+)/([\w-]+)/?$ index.php?ctrl=$1&act=$2&$3=$4&$5=$6&$7=$8&$9=$10 [L]
RewriteRule ^([\w-]+)/([\w-]+)/([\w-]+)/([\w-]+)/([\w-]+)/([\w-]+)/([\w-]+)/([\w-]+)/?$ index.php?ctrl=$1&act=$2&$3=$4&$5=$6&$7=$8 [L]
RewriteRule ^([\w-]+)/([\w-]+)/([\w-]+)/([\w-]+)/([\w-]+)/([\w-]+)/?$ index.php?ctrl=$1&act2=$2&$3=$4&$5=$6 [L]
RewriteRule ^([\w-]+)/([\w-]+)/([\w-]+)/([\w-]+)/?$ index.php?ctrl=$1&act=$2&$3=$4 [L]
RewriteRule ^([\w-]+)+/([\w-]+)?$ index.php?ctrl=$1&act=$2 [L]
RewriteRule ^([\w-]+)+/?$ index.php?ctrl=$1 [L]
The page can only called with controller and action. If one of them is missing, the user will be redirected. I set headers for that:
// Redirects
if (!isset($_GET) || !isset($_GET['ctrl'])) {
header('location: /index/index/');
}
if (!isset($_GET['act']) || (isset($_GET['act']) && empty($_GET['act']))) {
header("location: /".$_GET['ctrl']."/index/");
}
It's dirty i know. Generally it works, but there are some problems that i can't solve:
if i try to call the webapp with domain/ the page can't be displayed or i will be redirected to google with the search "domain/". If i type domain/controller/action it works.
If i add a trailing slash after the action, it ends up with 404. It should be the same behavior like without the slash at end.
If i add a slash before the path in header('location:... it works for the following pattern: domain/controller. It will be redirected to domain/controller/action (in this case controller/index).
But if i try domain/ it results in 404.
Without slash before:
domain/controller/ results in domain/controller/controller/action but domain/ will be redirected correctly to domain/index/index
What am i missing? :(
Thanks
Tyr

I think you forgot this:
rewritecond %{http_host} ^yoururl.com/ [nc]
rewriterule ^(.*)$ http://yoururl.com/$1 [r=301,nc]
This code needs to go under:
RewriteBase /

Related

How to mask the URL with htaccess in zend framework 1.10

Hi I Have to create the URL something like "http://www.contract.com/user/profileview/index/MQ==" to "http://www.contract.com/profile/india/MQ==". We have tried the masking with codeignitor and succeeded. But coming to the Zend it is throwing the error "Invalid controller specified (india)".
We have rewrite the htaccess rules to Mask the URL in codeignitor. Same is applied here, but it is not working here. My htaccess code is
#php_value magic_quotes_gpc off
RewriteEngine on
Options -Indexes
Options FollowSymlinks
RewriteEngine On
RewriteBase /
#Admin
RewriteRule ^admin(.*)$ public_mvc/admin.php [L]
#RewriteRule ^profile/(.*)/(.*)/(.*)(/|)$ user/profileview/index/$2 [L,NC]
RewriteRule ^profile/(.*)/(.*)/(.*)(/|)$ user/profileview/index/$2 [L,NC]
#RewriteRule ^profile/(.*)/(.*)/(.*)/?$ user/profileview/index/$2 [L,NC,QSA]
# Also Tried Ones. Start
#RewriteRule ^profile/(.*)/(.*)/(.*)/$ /user/profileview/index/$1 [NC,L]
#RewriteRule ^profile/(.*)/(.*)/(.*)/?$ /user/profileview/index/$2 [NC,L]
#RewriteRule ^profile/(.*)/(.*)/(.*) /user/profileview/index/$2 [NC]
#RewriteRule ^/profile/(.*)/(.*)/(.*)/?$ /user/profileview/index/$2 [QSA]
# Also Tried Ones. End
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ public_mvc/index.php
Rewriterule ^$ /home [r=301,nc]
What is the wrong with this? We have tried so many types options. But all went in vain. You can see all those different tries in the code. Please help me this.
Thanks in advance.
"http://www.contract.com/profile/india/MQ=="
above url can be rewriter in htaccess
method 1;
RewriteRule ^profile/(.*)$ redirectedUrl.php [NC,L]
redirectedUrl.php as page, it should be placed in the root directory, it cant shows an error, regards changing the url in htaccess!!!
method 2;
handling on bootstapper.php for redirect into some controller with another action for using
Zend_Controller_Router_Route_Regex
Thanks for the Support. I have achieved the goal in some other way. We have changed the ZEND routing according to our needs. So we have Auth file(which is called in every request). This file is called in all the requests. So what I did is I checked the URL and if the URL contains the module name as 'Profile' then I have set the Module, Controller and Action name using the "setModuleName", "setControllerName", "setActionName", etc..
$ModuleName = $request->getModuleName();
$ControllderName = $request->getControllerName();
$ActionName = $request->getActionName();
/*
This condition is added to mask the URL. This is added by Ghanta Kiran on 27-Dec-2013. Start
Example : (From) http://www.contractskills.com/profile/india/username/id ==> (To) http://www.contractskills.com/user/profileview/index/id
*/
if($ModuleName == 'profile')
{
$paramId = $request->getParam('id'); // This is to get the User Id from the URL, to fetch the user profile details.
$request->setModuleName('user');
$request->setControllerName('profileview');
$request->setActionName('index');
$request->setParam('id',$paramId);
}
So what it will do is, If the URL Contains the 'Profile' in it, it will set the Module, Controller, Actions explicitly. I have done so many changes to the .htaccess, but ultimately all those efforts went wild.

404 page when no match found

i have this rewrite rules:
RewriteEngine on
RewriteBase /
RewriteCond %{THE_REQUEST} \.php[\ /?].*HTTP/ [NC]
RewriteRule ^.*$ - [R=404,L]
RewriteRule ^(home|contact|about|submit|search)/?$ /index.php?page=$1 [L]
RewriteRule ^([a-z]+)/([a-z0-9-]+)/?$ /index.php?page=home&cat=$1&slug=$2 [L]
RewriteRule ^([a-z]+)/?$ /index.php?page=home&cat=$1 [L]
What i want is:
if user types something which is not matching last three conditions i want to show a 404 error. At present it loads home page. So i am check $_GET['page'] in there. I donot want to check in php.
do you have anything to improve my rewrite rules or any additions?
please help
Since everything is ultimately routing to index.php (Front-end Controller Pattern), I suggest dropping the the mod_rewrite and using:
FallbackResource /index.php
Then parse the URL in index.php and handle routing accordingly.
This will take some rewriting, but will be more flexible in the long run.
Read more about FallbackResource and Front-Controllers in PHP.
You may use mod rewrite to add argument to your web page index.php, then treating them in the php page possibly sending back a error page (as in cakePHP). that keeps only ONE rule.
RewriteRule ^([0-9A-Za-z]+)/?$ /index.php?argument=$1 [L]
Try to use ErrorDocumentwhit location page
ErrorDocument 404 /erreur_404.html

Seems like POST values are lost when .htaccess RewriteRule used. GET values are OK. How to fix?

Several days ago I had a question about removing index.php from the address bar, so the address of the page looks shorter and better. The shortest solution of this problem was (RewriteRule ^index.php / [L,R=301] in the .htaccess file). And it works!
Since I put that string into the .htaccess, some pages are redirected to the main page. I spent a lot of time to guess, why. As I understand, the answer is: with RewriteRule ^index.php / [L,R=301], $_POST parameters are not sent to the next page. $_GET parameters are OK.
Once I remove RewriteRule ^index.php / [L,R=301] from .htaccess, everything becomes fine as usual.
Why does it happen and how to fix that?
Thank you.
The [R] flag will incur a redirect. And user-agents issue a redirect as GET request. There is nothing that can be done if you really want to shorten URLs down to the / root path.
You could however block POST requests specifically from being rewritten/redirected:
RewriteCond %{REQUEST_METHOD} !POST
RewriteRule ^index.php / [L,R=301]
You could try using [L,R=307] instead. 307's must not change the request-method according to the spec, but I don't know how browser implemented 307.
But the root of the problem is the use of <form action="____/index.php" ...
Just leave the action empty to POST to the current url e.g.
I'm using something like:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/(css|images|js)/
# don't rewrite existing files, directories and links
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
# rewrite everything else to index.php
RewriteRule .* index.php [L]
</IfModule>
And its working for all requests, rewriting it via index.php file.
If you need to redirect 301 (which stands for Moved Permanently code) check out this question: Is it possible to redirect post data?
POST values will NEVER survive an external redirect (the R=301), it's a security liability, so browsers will never support that. Remove the R=301 and you will be fine. You just should alter all existing links to the page to the shorter/prettier one (<a>'s but also form actions etc.)
I had the same problems but my htacces was like this:
RewriteEngine on
RewriteRule .* index.php [NC]
Just change NC for L and everything works fine.
Final code:
RewriteEngine on
RewriteRule .* index.php [L]
In My case I used .htaccess.
Refer : PHP $_POST not working?
i.e
action="booking.php" to action="booking" worked for me

mod_rewrite: subdomain to controller

So I have mydomain.tld, www.mydomain.tld and res.mydomain.tld all pointing to the same directory: /var/www/mydomain. In that directory, there's my codeigniter application.
So what I'm trying to do is to forward all requests made through res.mydomain.tld to a specific controller called resources.
What I have:
RewriteCond %{HTTP_HOST} ^res\.mydomain\.tld$
RewriteRule ^(.*)$ /index.php?/resources/$1 [L]
This produces a server error, my rewrite log doesn't provide any clues about why; it just shows some very weird logic being applied to the request string.
Any idea of why this isn't working?
Leave your .htaccess was before.
In your routes.php
if($_SERVER["SERVER_NAME"]=="res.mydomain.tld"){
$route['default_controller'] = "resources";
}else{
//$route['default_controller'] = Your default controller...
}
You created a infinite loop. It keeps on rewriting, because the rules always match, and match again. Just add a rule like the following above your rule
RewriteRule ^index.php - [L]
This will prevent any remaining rules underneath it from executing if the (already rewritten) url starts with index.php
Make sure that index.php isn't matching and going into a rewrite loop:
RewriteCond %{REQUEST_URI} !^index\.php
RewriteCond %{HTTP_HOST} ^res\.mydomain\.tld$
RewriteRule ^(.*)$ /index.php?/resources/$1 [L]

mod_rewrite Problem - Routing pages to query string

I need help with my mod_rewrite for a site im currently working on.
Let's say I have this site http://example.com
And I want to be able to make any value after the / to route to page.php like below
http://example.com/value1
http://example.com/value2
to point to
http://example.com/page.php?id=value1
http://example.com/page.php?id=value2
,respectively.
But, not route to that page when im pointing to "admin"
http://example.com/admin/
I've tried
RewriteEngine on
RewriteCond $1 !^(admin)
RewriteRule ^(.*)$ /page.php?id=$1 [L]
But it isn't working. Any thoughts?
$1 is not available at the time of the Condition. I believe what you are looking for is close to:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} !^admin/.*
RewriteRule ^(.*)$ page.php?id=$1 [L]
this rule causes everything not starting with admin/ to go to /page.php. I don't believe the %{param} is optional. Using RewriteBase / means you do not to have prepend / on /admin and /page.php; it may actually fault if you use /page.php instead of page.php
If you have means of accessing the server values, then the final rule can be:
RewriteRule . page.php [L]
You can find the called url in the REQUEST_URI
This of any help?
.htaccess mod_rewrite - how to exclude directory from rewrite rule

Categories