I have this rewrite rule to access profile.php?user=username with mysite.com/username..
Problem with this is that it ignores my css, even if I use the full URL and whats weird is that its the same if i go to the regular url, profile.php?user=username. But If i remove my htaccess file the css works.
RewriteEngine on
RewriteRule (.*) profile.php?user=$1 [QSA,L]
Whats the problem here? Thanks in advance :>
You could use a condition to only fire your rule if the URL you are attempting to fetch doesn't resolve to a file or directory
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) profile.php?user=$1 [QSA,L]
This will stop a request for foo.css being rewritten as profile.php?user=foo.css
Your rule will rewrite any request, even the request for your css file.
You will also need a RewriteCond like this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) profile.php?user=$1 [QSA,L]
This will redirect only if there is no file that matches the request.
Remember that the request for the CSS file is a request too, and your rule is rewriting the request for whatever.css to profile.php?user=whatever.css
Related
In general, I am trying to understand how .htaccess works. I would highly appreciate it if anyone points me in the right direction. I have been trying to make the following url (with optional parameters) pretty.
mysite/v1.0/foldername
mysite/v1.0/foldername/param1/
mysite/v1.0/foldername/param1/param2/etc
I tried the following:
RewriteEngine On
RewriteCond %{REQUEST_URI} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)/?$ foldername.php [QSA,L]
the problem is that when I get it to pass the parameters it can no longer get the resources. It seems to have changed directory.
.htaccess is in foldername
Also, I would like to know what site i can go to to learn about REQUEST_URI, REQUEST_FILENAME, etc. A site that is not too technical as it's the apache site.
You are incorrectly rewriting the rules correct rule according to your need would be like,
RewriteEngine On
# rule for removing extension
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([\w-]+)/?$ $1.php [QSA,L]
# below cond means incoming url is nor a file neither a directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
# actual rules
RewriteRule ^([\w-]+)/([\w-]+)/?$ $1.php?param1=$1 [L]
RewriteRule ^([\w-]+)/([\w-]+)/([\w-]+)/?$ $1.php?param1=$2¶m2=$3 [L]
Refrences
Reference: mod_rewrite, URL rewriting and "pretty links" explained
http://httpd.apache.org/docs/current/mod/mod_rewrite.html
Lets say that I have a static app and a JSON file that feeds it.
The problem is that the JSON file it's not available yet and I can't use it, so, to make the application run in development environment, I want to add a rewrite rule to redirect the JSON request to a CI controller path that will return a JSON as well.
My htaccess file looks like this
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|images|assets|static|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
#Rewrite for development
RewriteRule ^static/app/json/appData.json$ /buscador/getAppData
The first condition is the CI main condition to redirect all request to index.php
Then I added the RewriteRule for the JSON file that's going to be located in static/app/json/appData.json and it needs to be redirected to the CI controller buscador/getAppData
The thing is that this Rewrite Rule isn't working and I don't know why, can somebody tell me what is wrong o how can I add a custom rewrite rule in CI?
Thank you!
I was pondering on a similar problem for quite a few hours. I needed to add a custom RewriteRile so that a shorter link should be generated (and working) in order to be sent as a footer in an SMS to users. I finally managed to do it with a 301 redirect before the CI redirects. Here's my .htaccess:
Options -Indexes +FollowSymLinks -MultiViews
RewriteEngine on
RewriteBase /
#Custom redirect
RewriteRule ^nv([0-9]+?)?$ /news/locate/item/$1 [NC,L,R=301]
#Remove trailing slash against SEO duplicates
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*)/$ /$1 [L,R=301]
#Generic controller redirects
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
Now http://domain.tld/nv1234 redirects to http://domain.tld/news/locate/item/1234 as intended, although not as transparently as I would like it to, but I couldn't find a better way.
Please note the R=301 in the Custom redirect line - the RewriteRule will not work if the client is not redirected to the newsController.
Posting my solution here with the hope that other developers will not get lost as I (and the OP) did.
I am trying to implement a front controller. So I want to redirect requests to index.php. Here is my .htaccess.
RewriteEngine On
# Redirect everything to the Front Controller
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule !^(css|images|files)/ index.php [NC,L]
Currently, if I visit the following url, it works:
http://devserver/myapp/
But, if I visit any url that does not resolve to index.php, for example http://devserver/app/blog, Apache tells me that it cannot find index.php:
The requested URL
/absolute/path/to/myapp/index.php
was not found on this server.
But that path actually exists on my server, so I think what's happening is Apache is trying to access that url like a browser would - and it doesn't work because if I type in :
/absolute/path/to/myapp/index.php in my browser, it doesn't work. So, I decided to change the last line in my .htaccess to this:
RewriteRule !^(css|images|files)/ http://myapp.com/index.php [NC,L]
But it still doesn't work, because now when I check for $_SERVER[REQUEST_URI] in my PHP code, it always resolves to index.php because Apache made a HTTP request.
Anyways, can someone please tell me what I'm doing wrong, and I what I can do to make Apache redirect everything to index.php?
Thanks,
Not sure but try this.
RewriteEngine On
# Redirect everything to the Front Controller
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
# RewriteRule !^(css|images|files)/ index.php [NC,L]
RewriteRule ^(.*)$ index.php/$1 [L]
I would like to be able to use rewrite rules to redirect anyone who opens the link :
domain.com/name
to
index.php?username=name
what would be the best way to do it?
I tried to post the htaccess code I wrote but stackoverflow keeps saying it doesn't meet standards so I removed it.
thanks in advance
Something like this will do it:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/([^/]+)/? [NC]
RewriteRule .* index.php?username=%1 [L,QSA]
It will map silently
http://domain.com/name
To
http://domain.com/index.php?username=name
For this to work, /name must be the first directory in the incoming URL path.
Quick question, and I've seen it asked hundreds of times, but I just can't seem to get it to work (shame on me).
I'm trying to redirect anything other than index.php to view.php?id=$1 where $1 is anything other than index.php - but I can't seem to get it to work.
For example:
http://domain.com/ should use index.php
http://domain.com/index.php as should this
but..
http://domain.com/sdgoi3 should use http://domain.com/view.php?id=sdgoi3 etc
I've tried a few things and gone down through the questions above but to no avail.
Anyone got a solution? Appreciated.
Try putting this in the htaccess file in your document root:
RewriteEngine On
RewriteRule ^$ /index.php [L]
RewriteRule ^index\.php - [L]
RewriteRule ^view\.php - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /view.php?id=$1 [L]
The logic here is:
if the request URI is / rewrite to index.php
if the request URI starts with index.php, don't change and pass through
if the request URI starts with view.php, don't change and pass through
if the request is to a non-existing file or directory, pass to view.php with the id param
Maybe something like :
RewriteCond %{REQUEST_URI} !^index\.php
RewriteCond %{REQUEST_URI} !^view\.php
RewriteRule ^(.*)$ view.php?id=$1 [L]
?