.htaccess multiple RewriteRule's doesn't work - php

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]

Related

.htaccess redirect directly after domainname

I want that if a user types in www.example.com/article-title, it gets the data from www.example.com/index.php?title=article-title. Right now it only works if a user types in www.example.com/article/article-title. I want to remove that article/.
This is what I have right now in my .htaccess file:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [NC]
RewriteRule ^article/([0-9a-zA-Z]+) index.php?title=$1 [NC,L]
If I remove article/ from the last line in my .htaccess file, it doesn't work at all.
The $1 should be article-title, but if I remove article/, $1 becomes index.
Does anyone know how I can change the .htaccess in order to let users type in www.example.com/article-title?
You just need
RewriteEngine on
RewriteRule ^(.*)$ index.php?title=$1 [NC]
tested here with
input: https://www.example.com/article-title
output: https://www.example.com/index.php?title=article-title
However i suppose this is actually what you really want, because by the looks of things you are not locking for a redirect, but something like:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
And then in the index.php file you have to "manipulate" the url requested

.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.

Redirect two files with htaccess

this is my scenario. My application urls are showed like this:
example.com/index.php/article/whatever/here
in order to remove the index.php from url I have put this in my .htaccess and works fine:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
Now, my problem is that I have include and admin.php like this:
example.com/admin.php/manage/whatever/here
And I don't know how to remove the admin.php from url without make conflics with the index.php because I have tried this and a 500 Internal Server Error is showed:
RewriteRule ^(.*)$ /index.php/$1 [L]
RewriteRule ^(.*)$ /admin.php/$1 [L]
EDIT
Another try was this and nothing, because a 404 error is showed:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/(.*?)/(.*) /$1/$2 [L]
ALL my urls should have several parameteres as requiered:
example.com/this/is/a/large/url/because/is/requiered
EDIT 2:
I have create two rules, each rule alone works fine!!! but both can't work together:
RewriteRule ^admin/(.*)$ /admin.php/$1 [L]
RewriteRule ^(.*)$ /index.php/$1 [L]
Try:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond ${REQUEST_URI} ^admin/(.*)$ [NC]
RewriteRule ^(.*)$ /admin.php/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
Then at least test this with:
http://www.example.com/some/url
http://www.example.com/admin/some/url
You can slightly modify your rewrite rule to capture the first part of the path, and use it in the replacement. Try this:
RewriteRule ^/(.*?)/(.*) /$1/$2 [L]
Basically the () create a capturing group, so anything between the first two / will be captured as $1 and anything after that will be captured as $2
Note that you can also change your rewrite conditions, if you only want this replacement to apply to index.php and admin.php

.htaccess problem

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]

Categories