mod_rewrite prefix problem - apache

I'm having some difficulties with mod_rewrite. My directory structure (part of it) is:
index.php
.htaccess
app
controllers
models
views
...
public
javascripts
stylesheets
foo
bar
So, as you can see, all static files are in app/public. That's the reason I would like to route requests like:
http://www.example.com/images/logo.png => app/public/images/logo.png
http://www.example.com/javascripts/app.js => app/public/javascripts/app.js
http://www.example.com/uploads/blog/something => app/public/uploads/blog/something
If that file doesn't exist, then it should route to index.php, like:
http://www.example.com/news/show/42 => index.php
(Because file app/public/news doens't exist)
So, this is my current .htaccess, but I think it can be done in more elegant way.
RewriteEngine on
RewriteRule (png|jpg|gif|bmp|ico)$ app/public/images/$1/$2.$3 [L]
RewriteRule ([^/.]+).js$ app/public/javascripts/$1.js [L]
RewriteRule ([^/.]+).css$ app/public/stylesheets/$1.css [L]
RewriteRule ^(.*)$ index.php [NC,L,QSA]
Also, problem with this one is, it only handles images, javascripts and stylesheets, but not all other files.
I know there is:
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
But, I don't know how to route them with prefix "app/public/"? I tried:
RewriteCond app/public/%{SCRIPT_FILENAME} !-f
RewriteCond app/public/%{SCRIPT_FILENAME} !-d
But it doesn't work! I also tried using RewriteBase, but it didn't work out.
EDIT:
I'm using mod_expires, and I'd really like to automate requests like:
http://www.example.com/images/logo.42.png => app/public/images/logo.png
http://www.example.com/javascripts/app.42.js => app/public/javascripts/app.js
So, as you can see, I would like to have optional number between filename and extension, and that number should be ignored (it is only used for future expires).
Previously, I found:
RewriteRule ^(.*)\.(\d+)(_m_\d+)?\.([^\.]+)$ $1.$4 [QSA]
But I don't really understand how it works and does it always work in my example (multiple extensions maybe)?
Thanks!

Try this instead:
/.htaccess
RewriteEngine on
RewriteRule ^$ app/public/ [L]
RewriteRule (.*) app/public/$1 [L]
/app/.htaccess
RewriteEngine on
RewriteRule ^$ public/ [L]
RewriteRule (.*) public/$1 [L]
/app/public/.htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
Finally, create an index.php in your /app/public/ and put a dispatcher here:
<pre>
<?php
function dispatch($action, $request) {
echo 'action: ' . $action;
echo "\nparameters: " ;
print_r($request);
}
$fragments = explode('/', $_GET['url']);
dispatch($fragments[0], array_slice($fragments, 1));
?>
</pre>
Now say try accessing:
# these will go through php
/controller/action
/post/create
/post/delete/1
# these are static files
/stylesheets/
/foo/bar/myfile.txt

Related

.htaccess to create SEO url not working

I have the file path: example.com/blog/ where blog is a directory. Blog gets a query string from the URL called URL and makes a dynamic page out of that information. So essentially it's something like this: example.com/blog?url=hello-world but i'd like to remove the ?url part and instead add a slash. So it should look something like this: example.com/blog/hello-world I've tried to accomplish this by putting the .htaccess file in the blog directory. This is my current .htaccess file, but it is not working:
.htaccess
RewriteEngine On
RewriteBase /stories/
RewriteCond %{THE_REQUEST} /\?url=([^&\s]+) [NC]
RewriteRule ^ %1? [L,R=302]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?url=$1 [L,QSA]
Inside /blog there is a index.php file and that generates the dynamic page.
RewriteEngine On # Turn on the rewriting engine
RewriteRule ^blog/([A-Za-z0-9-]+)/?$ blog/index.php?url=$1 [NC,L]
RewriteRule ^blog/?$ blog/index.php [NC,L]
Try this one.
EDIT
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(blog)/(.*)$ blog/index.php?url=$1

Using RewriteRule to write URL from page/unique_id to page.php?id=unique_id

I am trying to format my URL from http://jacqueskoekemoer.co.za/article/5 to http://jacqueskoekemoer.co.za/article?i=5 where i would be the article ID in the database.
Currently this URL works. But this one does not. It does change the url to article.php but it doesn't add the query string to the end.
I know this because it does not appear in print_r($_REQUEST); or print_r($_GET);
What am I doing wrong in my .htaccess file, or can you suggest other resources that I could look at to resolve the problem?
I have used the following 2 URL's primarily to get the RewriteRule working.
URL 1
URL 2
In .htaccess file the rule rewrite section looks like this:
RewriteEngine on
RewriteRule ^article/$ article.php?i=$1 [L] # Handle requests for "article"
#RewriteRule ^article/$/ article.php?i=$1 [NC,L] # Handle requests for "article"
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
Use the following rule:
RewriteEngine on
RewriteRule ^article/(\d+)$ article.php?i=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]

Unwanted result while using .htaccess for 2 rules

I'm working on a web application with PHP.
I'd like to have clean URLs, so I wrote a .htaccess file for it. Everything is ok, but just for 1 rule.
Look at the code below:
RewriteEngine on
Options +FollowSymLinks
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rule No.1
RewriteRule ^(.*)/(.*)/(.*)/?$ index.php?page=$1&id=$2 [NC,N]
# Rule No.2
RewriteRule ^(.*)/?$ index.php?page=$1 [NC,L]
This is my .htaccess file contents.
If I remove the Rule No.1, and the page be something like www.site.com/news, everything works fine. But when I want to navigate to page www.site.com/news/1/sample-text, all CSS, JavaScript, and image files will destroy (I have not access to them anymore).
Although I wrote Rule No.2 in .htaccess file, but anything did not work and the problem is steel alive.
All images, css and javascript paths are similar to ./js/jquery.js format.
Also, I put RewriteBase / in .htaccess file. But ... :(
Please help me.
Rules will be better if they are like this:
# Rule No.1
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/(.*)/(.*)/?$ index.php?page=$1&id=$2 [L]
# Rule No.2
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ index.php?page=$1 [L]
=> You need to repeat the RewriteCond lines here: they apply only to the RewriteRule which follows.
Notes:
NC flag (case insensitivity) is useless here
the first rule doesn't use $3
([^/]+) would make more sense than (.*)

htaccess issue causing internal server error too many redirects

so I am playing with .htaccess to have clean URLs in my codeigniter app.
in short, i am trying to:
1) remove index.php in urls (redirect permanent)
http://localhost/directory/index.php*
to
http://localhost/directory/*
http://my.domain.com/index.php*
to
http://my.domain.com/*
2) rewrite requests for certain controllers to index.php/[controller_name]
http://localhost/directory/controller1*
to
http://localhost/directory/index.php/controller1*
http://my.domain.com/controller2*
to
http://my.domain.com/index.php/controller2*
my htaccess file currently goes like this:
Options +FollowSymlinks
RewriteEngine On
#RewriteBase /
# Redirect index.php
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteCond %{THE_REQUEST} !/system/.*
RewriteRule ^(.*)index\.php((/)(.*))?$ /$1/$4 [R=301,L]
first issue:
this does not work for http://localhost/dir/index.php/controller1.
instead redirecting to http://localhost/dir/controller1, it redirects to http://localhost//controller1 ($1 return empty string?)
# Rewrite CI certain controllers
RewriteCond %{THE_REQUEST} directory/(home|other_controller) [NC]
RewriteRule ^(.*)(home|other_controller)(.*)$ /$1index.php/$2$3 [NC,L]
second issue:
this does not work for http://localhost/dir/home gives internal server error (too many redirects).
but if I test added R=301 code, it successfully redirect to http://localhost/dir/index.php/home. but this is not my intention to redirect, I only need to rewrite it.
please advise.. :)
Try with this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
You can place this in the directory the bootstrap file (index.php) is in.
If you have FastCGI implementation, you need to add a question mark in the rewrite rule:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]

Apache mod_rewrite query string before page and remove extention

I've scavenged the web for answers to my mod_rewrite woes and I feel I'm at the end of my wits. I will have a URL like such: http://www.website.com/dashboard.php?username=stackoverflow. This url isn't the prettiest of such. So my goal is to do a few things here...
Eliminate the extension of the php file (I've been able to do this so far with the code I'll show below, but I don't know that it will stay according to the other things I need to do)
Eliminate the "www" prefix
Move the username query string (only if it's "username", I don't want to match "id" or such) directly after ".com/"
Move the php filename (without the extension) after query string.
The final URL should look like such: http://website.com/stackoverflow/dashboard or perhaps http://website.com/stackoverflow/profile.
The code I have right now which eliminates the file extension is such:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
# remove .php ONLY if requested directly
RewriteCond %{THE_REQUEST} (\.php\sHTTP/1)
RewriteRule ^(.+)\.php$ /$1 [R=301,L,QSA]
# remove trailing slash ONLY if it is not an existing folder
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# rewrite to FILENAME.php if such file does exist and is not a folder
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ /$1.php [L,QSA]
Sadly this only fixes one of my issues and after looking at it, I'm getting the feeling there would be a better way to do this...
(1, 3, 4) As I understand you want to use pretty URLs, but backed by PHP pages. So it should be like this:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(\w+)/profile$ /dashboard.php?username=$1 [L]
It will make http://website.com/stackoverflow/profile working as http://www.website.com/dashboard.php?username=stackoverflow
(2)
# Redirect www.site.com to site.com with 301
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]