double redirect mod_rewrite - mod-rewrite

I am trying to configure a .htaccess file to my webpage to make it work as i want, but after a lot of searching and trying to understand the documentation, my head is just confused about these mod_rewrite.
It looks like this:
root/index.php
root/application/-subfolders-
root/config/-files-
root/library/-files-
what i want is everything sent to the index.php file as a parameter like index.php?page=$i, so i can have links like www.mypage.com/foo/bar and my index.php handles it.
ALSO i want to have the www.mypage.com/js/filename be sent to a subfolder in application,
like root/application/javascripts/filename.
I just cant get this to work, tried different answer i found both here and other places.
.htaccess at is now:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^/js/ application/javascripts/ [L]
RewriteRule ^/js/(.*)$ application/javascripts/$1 [L]
RewriteRule ^css/ application/css/ [L]
RewriteRule ^css/(.*)$ application/css/$1 [L]
RewriteRule ^images/ application/images/ [L]
RewriteRule ^images/(.*)$ application/images/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^.*$ index.php?page=$1 [NC]
</IfModule>

This should do it:
RewriteEngine on
RewriteRule ^js/(.*)$ application/javascripts/$1 [L]
RewriteRule ^css/(.*)$ application/css/$1 [L]
RewriteRule ^images/(.*)$ application/images/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?page=$1 [NC]

Related

mod rewrite how do i add multiple rules with different combinations

i have been pulling my hair out for 3 days. i ve checked forums and tutorials but i just dont get it. i have a url of
http://localhost/gallery/2012/2
but i also need to get
http://localhost/gallery/2
this is my htaccess file
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9_-]+)/([0-9-]+)/([0-9-]+)/?$ index.php?page=$1&date=$2&pagenum=$3 [L]
RewriteRule ^([a-zA-Z0-9_-]+)/([0-9-]+)/?$ index.php?page=$1&date=$2 [L]
RewriteRule ^([a-zA-Z0-9_-]+)/?$ index.php?page=$1 [L]
</IfModule>
how would i go about adding another rule
RewriteRule ^([a-zA-Z0-9_-]+)/([0-9-]+)/?$ index.php?page=$1&pagenum=$2 [L]

mod-rewrite weird behaviour on rewriterule

My .htaccess file looks like this:
Options +FollowSymlinks
RewriteEngine On # Turn on the rewriting engine
RewriteBase /
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
RewriteCond %{HTTPS} off
RewriteRule (.*) https://www.example.com%{REQUEST_URI} [R=301,L]
RewriteRule ^product/([0-9]+)/*. /tags.php?id=$1 [NC,L]
RewriteRule ^page/([a-zA-Z0-9_-]+)$ /site.php?page=$1 [NC,L]
I wanted the last rule to be:
RewriteRule ^site/([a-zA-Z0-9_-]+)$ /site.php?page=$1 [NC,L]
If I change the last rule, it does not work at all. I get a 404 not found.
I have no idea why it doesn't work, is the word 'site' a registered word or...?

Mod rewrite tags

my URL is like this.
http://site.org/index.php?page=tags&tag=tagname
I'd like to rewrite it to
http://site.com/tags/tagname
My current .htaccess
RewriteEngine On
# RewriteRule ^/?([a-z]+)$ $1.php [L]
RewriteEngine on
RewriteRule ^([A-Za-z0-9-_]+)$ index.php?paste=$1 [L]
RewriteEngine on
RewriteRule ^([A-Za-z0-9-_]+)/edit$ index.php?paste=$1&page=edit [L]
RewriteEngine on
RewriteRule ^([A-Za-z0-9-_]+).txt$ index.php?paste=$1&paste_txt=$1 [L]
RewriteEngine on
RewriteRule ^([A-Za-z0-9-_]+).txt$ index.php?paste=$1&paste_txt=$1 [L]
RewriteEngine on
RewriteRule ^([A-Za-z0-9-_]+)/delete$ index.php?paste=$1&page=delete [L]
RewriteEngine on
RewriteRule ^([A-Za-z0-9-_]+).download$ index.php?paste=$1&paste_download=$1 [L]
RewriteEngine on
RewriteRule ([^/\.]+)/?.cgi$ index.php?page=$1 [L]
#RewriteEngine on
#RewriteRule ([^/\.]+)/?.txt$ index.php?paste_txt=$1 [L]
#RewriteEngine on
#RewriteRule ([^/\.]+)/?.download$ index.php?paste_download=$1 [L]
RewriteRule ^/?([A-za-z0-9]+)/([A-za-z0-9]+)$ index.php?page=$1&tag=$2 [L]
I believe this should do it... That is if you want numbers in your tags. If not, remove the 0-9 from each part of it.

How do you remove index.php from a url using mod_write, while still allowing php to see the path with index.php in it?

For the past 3 days I have been playing around with Apache's mod_rewrite trying to get it to remove index.php from my url, while php still needs to see it in the path.
Essentially PHP needs to see this
http://example.com/index.php/Page/Param1/Param2
While the user needs to see this
http://example.com/Page/Param1/Param2
What I have right now is the following in an htaccess file
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [L,QSA]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)/index\.php [NC]
RewriteRule ^ /%1 [R=301,L]
Which was taken from another page, and is close to what I need. However this seems to be cutting off everything after the http://example.com/ part. How can I get mod_rewrite to show the user one thing and have php see something else?
This is the modified code you can use in your .htaccess (under DOCUMENT_ROOT) to remove index.php from URI:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (?!^index\.php)^(.+)$ /index.php/$1 [L,NC]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)/index\.php(/[^\s\?]+)? [NC]
RewriteRule ^ %1%2 [R=302,L]
Change R=302 to R=301 once you're satisfied that it is working fine for you.
This rule:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)/index\.php [NC]
RewriteRule ^ /%1 [R=301,L]
Needs to look like this:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\ /index\.php(.*)\ [NC]
RewriteRule ^ /%1 [R=301,L]
Also note that RewriteRule ^(.*)$ index.php?$1 [L,QSA] doesn't create a URI that looks like this /index.php/Page/Param1/Param2, it creates a query string that looks like this: /index.php?Page/Param1/Param2. Which isn't at all what you said PHP needs to see.

Question about url rewriting using apache mod_rewrite

I have a doubt about url rewriting using apache mod_rewrite. I am a newbie in mod_rewrite and I don't have any experience in regex.
What I want to do is to:
Rewrite / To /web/content/public/
Rewrite /clients/ To /web/content/clients/
How can I achieve above things.
I tried:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^/clients/$ web/content/clients/ [L]
RewriteRule ^(.*)$ web/content/public/$1 [L]
</IfModule>
But it doesn't work. What can I do?
^(.*)$ includes the slash. So don't include the slash in the rewritten pattern.
But include a root slash at the head of your rewritten pattern.
RewriteRule ^/clients(.*)$ /web/content/clients$1 [L]
RewriteRule ^(.*)$ /web/content/public$1 [L]
Check the apache access log and error log to see what kind of request URL comes back.
Please check the documentation, especially the list labeled "Here are all possible substitution combinations and their meanings:"
Try this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -l
RewriteRule ^ - [L]
RewriteRule ^/clients/$ web/content/clients/ [L]
RewriteRule ^/(.*)$ web/content/public/$1 [L]
And if you want to use that rules in a .htaccess file, remove the leading slash from the patterns.

Resources