I have a PHP script located at http://sb1.dev.codeanywhere.net/~a70097sb/hc/onlinestatus/image.php that requires two GET variables: ign and style. My .htaccess file is in the same directory as image.php.
I want requests of the form http://sb1.dev.codeanywhere.net/~a70097sb/hc/onlinestatus/{style}/{ign}.png to be rewritten to http://sb1.dev.codeanywhere.net/~a70097sb/hc/onlinestatus/image.php?style={style}&ign={ign}. How do I do this, as I have no experience with mod_rewrite, and wasn't able to get it to work using any of the tutorials I found on Google.
Tried this but it didn't work, I just get a 404 page:
Options +FollowSymlinks -MultiViews
RewriteEngine on
RewriteRule ^([^.]*)/([^.]*)\.png$ image.php?style=$1&ign=$2 [L,NC]
You're on the right track. The capture pattern ([^/]+) matches everything up to the next /, so you'll need two of those.
RewriteEngine On
RewriteRule ^([^/]+)/([^/]+)\.png$ image.php?style=$1&ign=$2 [L,NC]
Try something like that:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.mysite\.com [NC]
RewriteRule ^hc\/onlinestatus\/(.*)\/(.*)\.png /hc/onlinestatus/image.php?style=$1&ign=$2 [NC,L]
</IfModule>
Related
I want to make my actual url:
http://mywebsite.com/portfolio.php?cat=deportes
Into:
http://mywebsite.com/portfolio/cat/deportes
I've been trying some solutions given on here but none of them work. I can actually get rid of the .php, but do I have to change anything in the a hrefs ?
Try
RewriteEngine on
RewriteRule ^portfolio/cat/(.+) /portfolio.php?cat=$1 [L]
starkeen gave you a nice answer. I would suggest to do it for your whole project, not to create rewrite rules for every url you have.
You can have a .htaccess file like this
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>
And then to create your logic with php to parse the url itself and call the method / controller or whatever you wish.
Okay I'm trying to use Lando (landocms.com) and I'm trying to get the pretty urls option to work.
Basically by default Lando creates link like: domain.com/index.php/page. Supposedly, there is a way to remove the index.php so the links become: domain.com/page. I have created an .htaccess as directed, however it does not work.
Here is the .htaccess I am using:
<IfModule mod_rewrite.c>
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
I have tried alot of variations, /index.php/, index.php? and plenty more but none work. According to HostGator everything should be fine. Any thoughts? I think I'm going crazy haha.
Thanks!
Rewriting for a CMS is a two-tier approach. First, you need to set your .htaccess (I have put a safer one here for you):
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .+ index.php [QSA,L]
Then, LandoCMS allows you to remove the index.php from the generated addresses, by means of turning on the appropriate setting in the administration panel. See this link for more information.
If the .htaccess content I've given you doesn't work, then simply use the one that the CMS has given you.
You want to remove the index.php part from any URL, but process the incoming, friendly URLs through index.php nevertheless
RewriteEngine On
# remove index.php and redirect client
RewriteCond %{ENV:REDIRECT_SEO} ^$
RewriteRule ^/?index.php/(.*) /$1 [R,L]
# process friendly URL
RewriteCond %{REQUEST_URI} !^/index.php/
RewriteRule .+ /index.php/$0 [E=SEO:1,L]
The environment setting E=SEO:1 prevents an endless loop.
I have been trying multiple different type of example but I cant get it work when trying to add more folder and file as the exception for redirection. I just know this one example could work in my apache localhost:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule !^js($|/) http://google.com [L,R=301]
</IfModule>
So basically I want to redirect all except js,css,img folders and getOrder.php, getCustomer.php files, something like that. Could anyone help in this case?
I suggest you add a "last" rule that says "Don;t touch these folders". Then do your redirect after that.
Edit: Updated answer with the wrapper elements (FollowSymLink, ReWriteEngine on).
Options +FollowSymLinks
Options -Indexes
# These folders will not be touched.
RewriteEngine On
RewriteCond %{REQUEST_URI} "/css/" [OR]
RewriteCond %{REQUEST_URI} "/img/"
RewriteRule (.*) $1 [L]
# Then have your redirect rule
RewriteEngine On
RewriteRule (.*) http://google.com [L,R=301]
Although as suggested in the comments, you can have a redirect on "file not found" which may be more appropriate? Or put a redirect in the folders you don;t want someone to read. It's also nicer to redirect to a page on your saying saying "Not Found" than to redirect to google too. But that's your call.
i want rewrite it with a .htaccess i have this url:
../index.php?page=details&id=123456
like this:
../detail/123456
but I do not really know how to do this. Could you help me to rewrite this url?
or give me a simple example that I can understand how it works
RewriteRule ^(.*)/([0-9]+)$ index.php?page=$1&id=$2&%{QUERY_STRING}
The first (.*) selects any string, e.g. "details". ([0-9]+) catches a number, in your case "123456"
Finally
&%{QUERY_STRING}
ensures, that additional parameters are added to your backendscript, too.
This should work:
RewriteEngine On
RewriteRule ^detail/([^/]*)$ /index.php?page=details&id=$1 [L]
Here is an example I use for my webpage when it's in a subdirectory.
# "learn/degree/index.php?d=mathematics" shows as "learn/degree/mathematics"
RewriteRule ^learn/degree/([a-zA-Z_-]+)$ learn/degree/index.php?d=$1
To complete it, remember to write at the beginning of the page this:
RewriteBase /
RewriteEngine On
Here's the 'full' .htaccess I use in my page to give you some idea.
#Redirect any www. to the page without it. Helps to keep user logged.
RewriteBase /
RewriteEngine On
rewriteCond %{HTTP_HOST} ^www.newfutureuniversity.org [NC]
rewriteRule ^(.*)$ http://newfutureuniversity.org/$1 [R=301,L]
Options +Indexes
Options +FollowSymlinks
# Finally working. Rewrite the user so "/student/Username" will internally be "/student/?user=Username"
RewriteRule ^student/([a-zA-Z0-9_-]+)$ student/index.php?user=$1
# Rewrite the disciplines so "learn/degree/1" will internally be "learn/degree/index.php?dis=1"
RewriteRule ^learn/degree/([1-4])$ learn/degree/index.php?dis=$1
# Rewrite the degrees so "learn/degree/mathematics" will internally be "learn/degree/index.php?d=mathematics"
RewriteRule ^learn/degree/([a-zA-Z_-]+)$ learn/degree/index.php?d=$1
# Rewrite the degrees so "learn/subject/2/5/7" will internally be "learn/subject/index.php?class=2&div=5§ion=7"
RewriteRule ^learn/subject/([0-9])$ learn/subject/index.php?class=$1
RewriteRule ^learn/subject/([0-9])/([0-9])$ learn/subject/index.php?class=$1&div=$2
RewriteRule ^learn/subject/([0-9])/([0-9])/([0-9])$ learn/subject/index.php?class=$1&div=$2§ion=$3
You have to make the links to ../detail/123456, then the script interpretes it internally as ../index.php?page=details&id=123456. If you don't want this, then you are looking for a redirect.
Try this
<IfModule mod_rewrite.c>
RewriteEngine on
Options +FollowSymlinks
# RewriteBase / add this if necessery, commented intentionally
RewriteRule ^detail/([0-9]+)$ index.php?page=details&id=$2
</IfModule>
Hi guys I'm wondering how to make this kind of requests to the server I looked at many sites and they use this technique. For example gametrailers.com => http://www.gametrailers.com/video/level-six-diablo-iii/**721239**. I know that a GET request is made by using parameters like this: http://somesite.com/page.php?param=1. So how to make it like gametrailers. I hope you understand my question and I doubt that "721239" is a folder on the server with an index page inside of it.
You need to create a file placed in the folder near your script with name .htaccess
In this file you need to define rewriting rules. The contents of the file are:
RewriteEngine on
RewriteRule ^games/(.*)$ games.php?id=$1 [L]
In this case
http://somesite.com/games/213123
will be transformed into http://somesite.com/games.php?id=213123
The more convinient way is to do url rewriting. (wiki)
For example, you can have a .htaccess like this, well explained in this guide from symfony:
<IfModule mod_rewrite.c>
RewriteEngine On
# we skip all files with .something
RewriteCond %{REQUEST_URI} \..+$
RewriteCond %{REQUEST_URI} !\.html$
RewriteRule .* - [L]
# we check if the .html version is here (caching)
RewriteRule ^$ index.html [QSA]
RewriteRule ^([^.]+)$ $1.html [QSA]
RewriteCond %{REQUEST_FILENAME} !-f
# no, so we redirect to our front web controller
RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>
You can achieve this using MultiViews in Apache2 (http://httpd.apache.org/docs/2.0/content-negotiation.html).
MultiViews (when enabled) instructs Apache to build a parameter map when a resource doesn't exist, so for www.foo.com/app/game/14, if www.foo.com/app.php exists and app/game and app/game 14 don't, it can be set up to translate that to www.foo.com/app.php?type=game&article=14
Some people also use mod_rewrite, but I'm not sure that's the preferred approach.