mod_rewrite Problem - Routing pages to query string - php

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

Related

how to rewrite single URL using .htaccess

I need to rewrite only 1 specific URL, to display to visitors specific content: I tried something like, this:
RewriteEngine on
RewriteCond %{REQUEST_URI} example.com/test/2_5/page.html
RewriteRule ^(.*)$ example.com/tt.html [R,L]
I need to rewrite all requests to:
http://example.com/test/2_5/page.html
to
http://example.com/tt.html
how to do this?
thanks,
Redirect /test/2_5/page.html /tt.html
Ok, mod_rewrite
RewriteRule ^/test/2_5/page.html /tt.html [L]
Remove first / if using .htaccess in the site's root folder, not the .conf file. And, typically, the final url should be the full one, with http:// and domain, but this one will work too. If you want to do everything by the rules then
RewriteRule ^/test/2_5/page\.html$ http://example.com/tt.html [L]

Redirecting an entire directory excluding one particular url string

Greets:
I have an old piece of code in a /work directory that I have to keep there. I'd like to redirect all requests to the /work directory to the site root, *except for url's that look like this:
/work/index.php?option=com_career&view=career &
/work/administrator
How would I go about doing this in .htaccess?
Thanks in advance.
Chris
Try adding these rules to the htaccess file in your document root:
RewriteEngine On
# pass through this URL unchanged
RewriteCond %{QUERY_STRING} ^option=com_career&view=career
RewriteRule ^work/index\.php$ - [L]
# pass through this URL unchanged
RewriteRule ^work/administrator - [L]
# redirect everything else
RewriteRule ^work/ / [L,R=301]

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]

Categories