Combine htaccess rewrites - php

I am using php CodeIgniter and have the following rewrite rule in my .htaccess file:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
Since I want the users to use the site through SSL, I would like to add the following rewrite rule:
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.link.com/$1 [R,L]
However, when I do that, every page in a subdirectory will have /index.php/actual_page - the index.php before that is not desired. I thought that might come from having RewriteRule ^(.*)$ index.php/$1 [L] in there, but without the RewriteRule ^(.*)$ https://www.link.com/$1 [R,L] it works fine.
So, how can I rewrite the url to https without breaking the first rewrite?

Make sure the https redirect happens before the rewrite to index.php. You want it to look something like this:
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.link.com/$1 [R,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

Related

How to rename part of a url with .htaccess?

I currently have very little Apache experience, and am having difficulties with my .htaccess file. My question is this: how can I rename these files, listed below, properly? I believe my syntax is accurate, according to http://www.htaccesscheck.com, but when accessing these pages, either A: the page won't load due to a redirect loop, or B: the page won't load, but will redirect to the wrong page. Here is my current .htaccess file for this directory:
RewriteEngine On
RewriteBase /photos/
RewriteRule ^(.*)-(.*)$ archives.php?month=$1&year=$2 [L]
RewriteRule ^(.*)$ catpost.php?id=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-d [NC]
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteRule ^(.*)$ viewpost.php?id=$1 [QSA,L]
Any help is much appreciated.
Try code below:
RewriteEngine on
RewriteBase /photos/
RewriteRule ^([0-9]{1,2})-([0-9]{4})$ archives.php?month=$1&year=$2 [L]
RewriteRule ^([0-9]+)$ catpost.php?id=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-d [NC]
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteRule ^(.*)$ viewpost.php?id=$1 [QSA,L]
This rules will check, if:
request like yourdomain/11-1111, then return archives.php
request like yourdomain/111, then return catpost.php (you can type
any number)
else will return viewpost
You have some errors in your current .htaccess, because your second rule get result of first rule.
By the way, you can use http://htaccess.madewithlove.be/ to check step by step what is posted to your rewrite rules.
be sure to write a valid pattern
try this
RewriteEngine On
RewriteBase /photos/
RewriteRule ^(.*?)-(.*?)$ archives.php?month=$1&year=$2 [L]
RewriteRule ^(.*?)$ catpost.php?id=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-d [NC]
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteRule ^(.*?)$ viewpost.php?id=$1 [QSA,L]

page slug with rewriterule in htaccess

I'm using Rewrite Rule in htaccess to get page data by slug.
I'm doing it manually by adding lines to .htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} !www\.example\.co\.il$ [NC]
RewriteRule ^(.*)$ http://www.example.co.il/$1 [L,R=301]
ErrorDocument 404 /404.php
RewriteRule ^עמוד-מסוים$ /page.php?id=1 [L]
RewriteRule ^דוגמא-לעמוד$ /page.php?id=2 [L]
RewriteRule ^דוגמא-נוספת$ /page.php?id=3 [L]
RewriteRule ^טקסט-כלשהו$ /page.php?id=4 [L]
RewriteRule ^צרו-קשר$ /contact.php [L]
RewriteRule ^blog$ /blog.php [L]
It's work good but I need to add new .htaccess line every time.
my DB table has Id and Slug (and other info)
and my page.php use $_GET['id'] and then select the other data from DB by this ID.
I tried somthing like this:
.htaccess:
RewriteRule ^page/([^/]*)$ /page.php?id=$1 [L]
page.php:
$id=$_GET['id'];
if(is_int($id)==false){ // if slug was enterd
$result=$mysqli->query("SELECT `id` FROM `pages` WHERE `slug`='$id' limit 1");
while($row=mysqli_fetch_assoc($result)){
$id=$row['id'];
}//query
}
But the URL not look like I want (adding /page/ to url)
I tried that also:
.htaccess:
RewriteRule ^/([^/]*)$ /page.php?id=$1 [L]
but it's make problem with other url's on my site (that are not connected to page.php)
How can I make it work without adding htaccess line every time?
EDIT:
when I try this .htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} !www\.example\.co\.il$ [NC]
RewriteRule ^(.*)$ http://www.example.co.il/$1 [L,R=301]
ErrorDocument 404 /404.php
RewriteRule ^/?([^/]+)$ /page.php?id=$1 [L]
RewriteRule ^צרו-קשר$ /contact.php [L]
RewriteRule ^blog$ /blog.php [L]
I get error on all pages, If I moving up the RewriteRule ^/?([^/]+)$ /page.php?id=$1 [L] only the page.php file will work good (blog and contact will not work) and also non-WWW url's will not work good.
You could try adding some conditions, something like:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?([^/]+)$ /page.php?id=$1 [L]
to filter out all existing files and directories.
Also, you might want to consider sanitizing your $id parameter before you stick it in a query
Found the soultion. This working perfect:
ErrorDocument 404 /404.php
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteRule ^צרו-קשר$ /contact.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?([^/]+)$ /page.php?id=$1 [L]

url rewriting not working on one.com provider

Currently i am trying to get url rewriting to work on one.com hosting provider which is a pain and support don't understand the issue, so i was thinking of asking here if you may assist me finding the issue.
I'm using this rewriterule which worked on localhost but not with the provider:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?key=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ index.php?key=$1
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ index.php?key=$1&seo=$2
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/$ index.php?key=$1&seo=$2
</IfModule>
RewriteCond %{HTTP_HOST} ^mysite\.com
RewriteRule ^(.*)$ http://www\.mysite.com/$1 [R=permanent,L]
and in index:
if($key=='post') //Cant include $seo here because the variable differ for
//each post on the site
{
include('post.php'); // Post page
}
Then in post.php ofc i take the seo GET variable to display the post content on the page.
complete url www.mysite.com/post/test-test-test
The error i end up with is 404
Not Found
The requested URL /post/test-test-test.php was not found on this server.
So basically what i understand is that it tries to enter a folder named /post/ and look for the file test-test-test.php
So i believe it doesn't include the rewrite rule, or can you find an error in the rewrite rule which worked on localhost?
You need to rearrange your rules and for adding .php extension make sure a file with .php exists:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^mysite\.com$ [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L,NE]
# ignore rest of the rules for files and directories
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^([\w-]+)/?$ index.php?key=$1 [L,QSA]
RewriteRule ^([\w-]+)/([\w-]+)/?$ index.php?key=$1&seo=$2 [L,QSA]
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]
</IfModule>

.htaccess multiple rules and exceptions

I have a .htaccess file that has multiple rules to make the url's look "pretty".
This is the file:
RewriteEngine On
RewriteRule ^property/([^/]*)/?([^/]*)/?$ /property.php?ID=$1&Image=$2 [L]
RewriteRule ^property$ /property.php [L]
RewriteRule ^enquire/([^/]*)/?([^/]*)/?$ /enquire.php?ID=$1&Data=$2 [L]
RewriteRule ^enquire$ /enquire.php [L]
RewriteRule ^contact/?$ /contact.php [L]
RewriteRule ^home/?$ /index.php [L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_URI} ^/(property|property/.*|enquire|enquire/.*|contact|contact/.*|home|home/.*)$
RewriteRule ^([^/]*)$ /custompages.php?ID=$1 [L]
The first set of rules works for property, enquire, contact and home.
If the url is not one of these, e.g. www.foo.com/about-us, I want it to call the file custompages?ID=about-us but with this code, it isn't working. I am quite new to using .htaccess files and I can't figure out what the issue is myself.
Your last rule is not correct. Change that to:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ /custompages.php?ID=$1 [L]

Redirect two files with htaccess

this is my scenario. My application urls are showed like this:
example.com/index.php/article/whatever/here
in order to remove the index.php from url I have put this in my .htaccess and works fine:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
Now, my problem is that I have include and admin.php like this:
example.com/admin.php/manage/whatever/here
And I don't know how to remove the admin.php from url without make conflics with the index.php because I have tried this and a 500 Internal Server Error is showed:
RewriteRule ^(.*)$ /index.php/$1 [L]
RewriteRule ^(.*)$ /admin.php/$1 [L]
EDIT
Another try was this and nothing, because a 404 error is showed:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/(.*?)/(.*) /$1/$2 [L]
ALL my urls should have several parameteres as requiered:
example.com/this/is/a/large/url/because/is/requiered
EDIT 2:
I have create two rules, each rule alone works fine!!! but both can't work together:
RewriteRule ^admin/(.*)$ /admin.php/$1 [L]
RewriteRule ^(.*)$ /index.php/$1 [L]
Try:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond ${REQUEST_URI} ^admin/(.*)$ [NC]
RewriteRule ^(.*)$ /admin.php/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
Then at least test this with:
http://www.example.com/some/url
http://www.example.com/admin/some/url
You can slightly modify your rewrite rule to capture the first part of the path, and use it in the replacement. Try this:
RewriteRule ^/(.*?)/(.*) /$1/$2 [L]
Basically the () create a capturing group, so anything between the first two / will be captured as $1 and anything after that will be captured as $2
Note that you can also change your rewrite conditions, if you only want this replacement to apply to index.php and admin.php

Categories