.htaccess Redirect - .htaccess

I need to create a redirect that sends the user to a specified php page with the variable of the page they originally requested, such as:
http://website.com/4
would send them to
http://website.com/download.php?id=4
However I don't want to redirect them if they request an actual page in the root directory, such as website.com/index.php.
Any idea how to accomplish this?

Should be exactly what you asked for; rewrites the URL to include download.php?id= unless the request is for any file that physically exists already:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ download.php?id=$1 [L]
Edit: I added the RewriteEngine On because it may not work without it depending on your server setup. To be fair, mlerley's answer reminded me that it should be there.

Assuming Apache 2.2:
RewriteEngine On
RewriteRule ^(\d+)$ /download.php?id=$1
The url doesn't change in this case. This also assumes that your id is always a number.

Related

Is there a way to add IF to .htaccess?

I'm building a web-builder, but I have no idea how to use .htaccess. Is there a way to make something like that (I'll write it in php because my English bad so it is easier to me):
<?php
if(ERROR && ONE_NAME){ //There is no page like that, for example: www.example.com/thispageisntexists
//redirect to page /website.php?webname=thispageisntexists
}elseif(ERROR && TWO_OR_MORE){ //for example: www.example.com/thispageisntexists/hi.php
//redirect to page /website.php?webname=thispageisntexists&pagename=hi
//if url is www.example.com/thispageisntexists/hi.php#hi rediract to /website.php?webname=thispageisntexists&pagename=hi#hi
//if url is www.example.com/thispageisntexists/hi.php?name=vlad&last=gincher rediract to /website.php?webname=thispageisntexists&pagename=hi$name=vlad&last=gincher
//and so on...
}
?>
inside website.php I'll check if the page exist and if no it will redirect to 404.php, and if it exist it will show the website.
I find it somewhat unclear what url's should be redirected or rewritten. I am assuming you have (or want) seo urls like this http://example.com/aaa and http://example.com/aaa/bbb and want to internally rewrite that to something that makes sense to the server.
You want it to work if there is already a query string present. This means that you have to concat the two query strings with the [QSA] flag.
"There is no page like that" translates to a condition that checks if the requested filename exists. You can negate that condition with a ! before the second argument. That would be RewriteCond %{REQUEST_FILENAME} !-f.
#hi is an anchor. It is never sent to the server. Your browser should however automatically keep appending that to the url, even when redirecting the request.
I see that Jon Lin already posted a .htaccess file that should work for you. I will still post this answer to clarify what is used there.
Try:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/website\.php
RewriteRule ^([^/]+)$ /website.php?webname=$1 [L,R,QSA]
# you can remove the "R" if you don't want to redirect the browser
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/website\.php
RewriteRule ^([^/]+)/(.*?)(\.php)?$ /website.php?webname=$1&pagename=$2 [L,R,QSA]
# you can remove the "R" if you don't want to redirect the browser
There's nothing with such an explicit IF-THEN-ELSE. .htaccess does have ways of doing conditional URL/URI redirects and rewrites, using RewriteCond and RewriteRule commands. They use "regular expressions" to match patterns, so they aren't as flexible as a real scripting language like PHP, but you can do lots with them.
RewriteEngine On
RewriteCond %{REQUEST_FILE} !-f
RewriteRule /?(.*)$ /website.php?webname=$1 [L]
might be a start for what I understand you're trying to do.

Htaccess rule to make urls like this ?page=1 look like /page/1 in Codeigniter

This is how my urls currently look:
http://mysite.com/?page=1
How can I make this work?:
http://mysite.com/page/1
There is a post on StackOverflow that asks the same question. But the accepted solution isn't working for me. Because I am using Codeigniter and my page results in a 404 perhaps because since the url pattern of a CI site is:
domain/controller/method
The system is assuming that I am requesting a controller called "page" and a method called "1" both of which of course doesn't exist. Or maybye it's due to a conflict with the other code in my htaccess file (which I downloaded from the CI wiki, it gets rid of index.php and does a few security things). Here is my entire htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
#Removes access to the system folder by users. Additionally this will allow you to create a System.php controller, 'system' can be replaced if you have renamed your system folder.
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
#When your application folder isn't in the system folder. This snippet prevents user access to the application folder. Rename 'application' to your applications folder name.
RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
#Checks to see if the user is attempting to access a valid file, such as an image or css document, if this isn't true it sends the request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
#Pretty urls for pagination links
RewriteRule (.*) index.php?page=$1
</IfModule>
The non indented bit is the solution I got from that other SO question that isn't working for me.
Any solutions to this CI pagination issue?
UPDATE
Ok, read some of the docs and now I have this working:
http://mysite.com/home/index/2
What would be the htaccess rule to turn that into?:
http://mysite.com/page/2
You should make this configuration at /application/config/routes.php (and let the .htaccess just for hide the index.php as you are already doing).
$route['page/(:any)'] = 'home/index/$1';
Or better, like #zaherg remembered (ensures that only numbers could by matched):
$route['page/(:num)'] = 'home/index/$1';
This way all the requests to http://mysite.com/page/2 will be treated internally as http://mysite.com/home/index/2 and so forth.
I suggest you take a look at CodeIgniter User Guide - URI Routing and CodeIgniter User Guide - Tutorial − Introduction.
Good luck.
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
from CodeIgniter docs
That will handle removing the index.php, but what happens after that depends how CodeIgniter's query string handling is set up: it can be configured to use a query string rather than a path. See the link for more details.

How to avoid URL and directory match problem when editing .htaccess file?

I have the following lines in the .htaccess file in the site directory:
RewriteEngine On
RewriteRule ^([a-z]{1}[a-z0-9_-]{3,20})$ account.php?username=$1&%{QUERY_STRING}
If it receives URL for example :
http://localhost/samplesite/johnsmith
it will rewrite it to
http://localhost/samplesite/account.php?username=johnsmith
which is fine.
The problem occurs when there is a directory named johnsmith in the site directory. then the URL is rewritten to and displayed as
http://localhost/samplesite/johnsmith/?username=johnsmith
and that is a problem. I am trying to implement account pages functionality for every user but if a user wants to register a username like some directory in the root the functionality will break? I tried adding rewrite conditions to check if the requested URL stands for an existing directory or a file :
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
but I don't know how to proceed.
If someone knows a better way to do account pages functionality for users I would appreciate to give me a piece of advice on that.
Can anybody help me solve this case? Thank you!
Your RewriteCond will work if it is in the correct place. Your full .htaccess should look like:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
# ---------------------------------------------------------------See info below--
RewriteRule ^([a-z]{1}[a-z0-9_-]{3,20})$ account.php?username=$1&%{QUERY_STRING}
Also, you don't need the %{QUERY_STRING}. Instead you should use the QSA flag to append the rest of the query string...
RewriteRule ^([a-z]{1}[a-z0-9_-]{3,20})$ account.php?username=$1 [L,QSA]

ignore specific directories in htaccess using mod_rewrite

I've got the following code in my .htaccess to strip out index.php from the urls in my CMS-based site.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
This code works great and it routes requests exactly how I want. For example, with URL: http://example.com/contact/ the directory contact doesn't actually exist if you look in the FTP; instead index.php handles the request and shows my contact info. Perfect. Well, almost perfect.
I want to modify this code to specify a couple directories in FTP that should be ignored. For example, if I've got a folder called assets, when I go to http://example.com/assets/ the default DirectoryIndex page is displayed. Instead, I want this directory to be ignored -- I want index.php to handle /assets/.
TL;DR: How can I modify the above code to explicitly ignore certain existing directories (so that index.php handles them instead of the DirectoryIndex)?
Why not adding this below or before your code?
RewriteRule ^(assets/.*)$ /index.php/$1 [L]

How do I redirect all but one url to a script

I'm trying to get www.example.com and www.example.com/index.html to go to index.html, but I want all other urls e.g. www.example.com/this/is/another/link to still show www.example.com/this/is/another/link but be processed by a generic script. I've tried
RewriteEngine on
RewriteCond %{REQUEST_URI} !^index\.html$
RewriteCond %{REQUEST_URI} !^$
RewriteRule ^(.*)$ mygenericscript.php [L]
but it wont work, can someone please help?
Instead of testing what %{REQUEST_URI} is, you can instead just test if the resource exists:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* mygenericscript.php
This prevents your static resources (images, stylesheets, etc.) from being redirected if they're handled through the same directory your .htaccess is in as well.
What's probably happening now is that you're seeing an internal server error, caused by an infinite internal redirection loop when you try to access anything that isn't / or /index.html. This is because .* matches every request, and after you rewrite to mygenericscript.php the first time, the rule set is reprocessed (because of how mod_rewrite works in the context that you're using it in).
The easiest to do this is to install a 404-handler which gets executed when the server does not find a file to display.
ErrorDocument 404 /mygenericscript.php
or
ErrorDocument 404 /cgi-bin/handler.cgi
or similar should do the trick.
It is not that RewriteRule's can not be used for this, it is just that they are tricky to set up and requires in depth knowledge on how apache handles requests. It is a bit of a black art.
It appears as if you're using PHP, and you can use auto_x_file (x is either append or prepend:
http://php.net/manual/en/ini.core.php

Resources