.htaccess problem - php

I want to redirect what's in link after localhost/myScript/ to localhost/myScript/test.php?var=$1.
So.. something like
localhost/myScript/this/is/just/a/test/
(with or without the last slash) would redirect to
localhost/myScript/test.php?var=this/is/just/a/test
This is what I've got, but it's not good enough.
RewriteRule ^(.+)$ test.php?var=$1
I've tried using
RewriteRule ^(.+)/?$ test.php?var=$1
But, I get $_GET['var'] = 'test.php'.

I see 2 main approaches:
1. Two separate rules to deal with URL with and without trailing slash. It will only rewrite requests to non-existing files/folder thus preventing rewrite loop you are having:
# work with URL that ends with slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ test.php?var=$1 [L]
# work with the rest of URLs (that ends with no slash)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ test.php?var=$1 [L]
2. Have separate rule to ignore existing files (to not to rewrite already rewritten URLs) and then use 1 line rewrite rule for URL with and without trailing slash:
# do not do anything for already existing files
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .+ - [L]
# rewrite URLs
RewriteRule ^(.*[^/])/$ test.php?var=$1 [L]
RewriteRule ^(.+)$ test.php?var=$1 [L]
I would prefer #2.

this is better one
RewriteCond %{REQUEST_URI} !\.php$
RewriteRule ^(.+?)$ test.php?var=$1 [L]

Related

.htaccess multiple RewriteRule's doesn't work

The multiple RewriteRule's doesn't work into my .htaccess file.
To get direct into the point, i have this lines of code into my .htaccess file
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ ./index.php?lang=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)/$ ./profile.php?page=$1 [L]
The problem is when i add a parameter into my domain, let's suppose www.eaxmple.com/something, i land always to home page. What i want to do is when i set a parameter with slash at the end to go to profile.php and without slash to move into index.php. Even if i tried to put a parameter i always move to index page.
Can someone help me?
.* will match everything including trailing /.
Try rules in this order:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L][
RewriteRule ^(.+)/$ profile.php?page=$1 [L,QSA]
RewriteRule ^(.*)$ index.php?lang=$1 [L,QSA]

htaccess rewriting URLs with multiple forward slashes

Trying to work out a set of .htaccess mod_rewrite rules that will set these links:
domain.com/content-page
domain.com/region/another-page
domain.com/region/brand/more-content-here
to fetch files:
domain.com/content-page.php
domain.com/region-another-page.php
domain.com/region-brand-more-content-here.php
Where 'region' and 'brand' and the page name are all variable/changeable.
So far I have:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,C]
RewriteRule ([^/]+)/([^/]+) $1-$2 [NC,L]
This works but only for one forward slash. As mentioned above, URLs after the domain may have none, one or two forward slashes. Haven't had any luck trying to extend this to deal with multiple (or not) slashes.
Try:
RewriteEngine On
# pass through root
RewriteRUle ^(index\.php)?$ - [L]
# no more / so add extension
RewriteCond $1 !/
RewriteCond $1 !\.php$
RewriteCond ${REQUEST_FILEAME} !-f
RewriteRule ^(.*)$ /$1.php [L]
RewriteCond %{REQUEST_URI} !\.php$
RewriteRule ^(.*)/(.*)$ /$1-$2 [L]

.htaccess - grab entire url while placing last section -([0-9]{1,5}) into second variable

Unlike most htaccess requests, where I can simply use the following code to grab the entire URL.
RewriteRule ^(.+)$ /index.php?url=$1 [L,QSA]
What I need to do, is add paging without the use of a directory ( /page/# ) in the address. So unlike /page/# in the url, the paging is just adding a dash and a number at the end of the url, such as " -3 for page three, etc.
I've tried several rewrite Rules, but I don't believe I understand apache rewriting well enough because my regular use of paging, doesn't work when applying a catchall expression.
RewriteRule ^(.+)-([0-9]{1,5})$ /index.php?do=something&url=$1&page=$2 [L] ## Error In? (.+)?
RewriteRule ^(.+)$ /index.php?do=something&url=$1 [L,QSA] ## Works
In the other urls, which contain direct directories such as "something", the paging will work fine.
RewriteRule ^something/([a-z]{1,6})/(.*)-([0-9]{1,5})$ /index.php?do=first&what=$1&url=$2&page=$3 [L]
What do I have to fix in the code for catchall paging. What am I missing? The address WILL have dashes, and sometimes slashes for directories.
ERROR Code:
RewriteRule ^(.+)-([0-9]{1,5})$ /index.php?do=this&url=$1&page=$2 [L]
Currently looks exactly like the following:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)-([0-9]{1,5})$ /index.php?do=lists&url=$1&page=$2 [L]
RewriteRule ^(.+)$ /index.php?do=lists&url=$1 [L,QSA]
If I remove The line "RewriteRule ^(.+)-([0-9]{1,5})$ /index.php?do=lists&url=$1&page=$2 [L]" - everything works fine.
RewriteCond is only applicable to very next RewriteRule. Try this code:
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^(.+?)-([0-9]{1,5})$ /index.php?do=lists&url=$1&page=$2 [L,QSA]
RewriteRule ^(.+)$ /index.php?do=lists&url=$1 [L,QSA]
AHh I see now. The RewriteCond before makes ALL the difference. A set of rewrite conditions only applies to a single redirect rule. You may need to do this:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)-([0-9]{1,5})$ /index.php?do=lists&url=$1&page=$2 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ /index.php?do=lists&url=$1 [L,QSA]

How to force .htaccess used for routing to not route .css, .js, .jpg, etc. files?

I have the following .htaccess file in a subdirectory of a site which allows me to route all URLs to index.php where I can parse them.
However, it's not allowing the standard files that I need for the website, e.g. css, javascript, pngs, etc.
What do I need to change (I assume in the fourth line) to allow these files so they don't get routed to index.php?
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|public|css|js|png|jpg|gif|robots\.txt)
RewriteRule ^(.*)$ index.php/params=$1 [L,QSA]
ErrorDocument 404 /index.php
Something I noticed. You're using the forward slash instead of a question mark... the params redirect would normally look like this:
RewriteRule ^(.*)$ index.php?params=$1 [L,QSA]
This should work by itself since any of those files *should* be real files.
ErrorDocument 404 /index.php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?params=$1 [L,QSA]
To have the site ignore specific extensions you can add a condition to ignore case and only check the end of the filenames in the request:
RewriteEngine On
RewriteCond %{REQUEST_URI} !(\.css|\.js|\.png|\.jpg|\.gif|robots\.txt)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?params=$1 [L,QSA]
If you're trying to ignore a folder then you could add:
RewriteEngine On
RewriteCond %{REQUEST_URI} !(public|css)
RewriteCond %{REQUEST_URI} !(\.css|\.js|\.png|\.jpg|\.gif|robots\.txt)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?params=$1 [L,QSA]
The easiest is to ignore them explicitly early in your rules:
RewriteRule \.(css|js|png|jpg|gif)$ - [L]
RewriteRule ^(index\.php|robots\.txt)$ - [L]
This avoid carrying them around all over the place with RewriteCond.
At your option, check that the file exists prior to doing so:
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule \.(css|js|png|jpg|gif)$ - [L]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(index\.php|robots\.txt)$ - [L]
(Note that the file check generates an extra disk access.)
You have the right idea, tweaking the fourth line.
The ^ is saying the various matching strings must be at the beginning of the line. If you don't care where any of these appear in the file, you can just remove the ^. That will avoid rewriting *.css, *.js, etc.; but will also not rewrite publicideas.html.
If you want to limit to just the suffixes, try this:
RewriteCond $1 !^(index\.php|public|.*\.css|.*\.js|.*\.png|.*\.jpg|.*\.gif|robots\.txt)$
This says to match anything at the beginning, then a ., then the suffix. The $ says match these at the end (nothing following).
I'm not sure about the public, so I left it (which means exactly public, with nothing else - probably not what you meant, but you can add a * before or after, or both).
RewriteCond %{REQUEST_FILENAME} !-f alone should be enough.
Another option is t exclude specific files from the rewrite.
From the TYPO3 packages:
# Stop rewrite processing, if we are in the typo3/ directory.
# For httpd.conf, use this line instead of the next one:
# RewriteRule ^/TYPO3root/(typo3/|t3lib/|fileadmin/|typo3conf/|typo3temp/|uploads/|favicon\.ico) - [L]
RewriteRule ^(typo3/|t3lib/|fileadmin/|typo3conf/|typo3temp/|uploads/|favicon\.ico) - [L]
This rule should appear before your actual rewrite. It should be
RewriteRule ^(public/|*\.css|*\.js|*\.png|*\.jpg|*\.gif|robots\.txt) - [L]
in your case.

Having problems adding '/' to end of URI

Right now i have a url which is like
http://www.example.com/customer/login
i want the URI to always have a ending trail because ill use redirects with ../
and if it doesnt have slash it messes everything up if it has a slash it works fine. I tried to look at some examples online but i couldnt really get anything to work heres my current .htaccess file
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?rt=$1 [QSA]
Anubhava's got the basic answer and I modded him up. You need to send an HTTP redirect to get the browser to request the URL with the / at the end.
To merge with your existing rewrite rules, you should do:
RewriteEngine on
# First check it's not a file
RewriteCond %{REQUEST_FILENAME} !-f
# And it doesn't end in /
RewriteCond %{REQUEST_URI} !.*/$
# Send the redirect. I would do 301 (permanent) here
# the "L" means the rest of the rules are ignored for this request
RewriteRule ^(.*)$ $1/ [L,R=301]
# Now pass thru to your old ruleset URLs the slash-checker didn't catch
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?rt=$1 [QSA]
How about this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ $1/ [L,R=302]
Try using following rules
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?rt=$1 [QSA]
#following rule checks if url is not ending with / and
#if not then redirected to url which is ending with /
RewriteCond %{REQUEST_URI} !.*/$
RewriteRule ^(.*)$ $1/ [R=302,L]

Categories