PHP regex for .htaccess - php

Say I have a page testpage.php and I want to access it through localhost/testpage . I can't figure out the regex to get it to work.
When I use the following, I get a server error
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* "^[^0-9][A-z0-9_]+*.php"/$0
However, when I specify the exact file it works.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* testpage.php/$0

Try this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
the third line checks if the file actually exists before doing the rewrite to avoid errors

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php$
RewriteRule ^(.*)$ $1.php [L]
It will rewrite the filename with .php extention

You cannot specify a regular expression in the replacement pattern. That's why it fails.
In this line
RewriteRule .* "^[^0-9][A-z0-9_]+*.php"/$0
You are telling Apache to rewrite anything ( .* ) to the literal "^[^0-9][A-z0-9_]+*.php"/, plus the zeroth matched string (which is an error anyway, because matches start from $1, then there is $2 etc.).
If you want to capture a match you must surround it with parentheses, like
(.*)
and then it will be available in the replacement string as $1.

Related

Passing Different Parameter for Same File using URL Rewrite

I have user.php file and I m passing two parameters with it using URL Rewrite
but I m unable to use this rule as whichever rule is written first only gets executed and not the second one in case of my rule for userID rule doesn't work but work for webName. Any solution will be very much helpful to me.Thank you
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^user/(.*)$ user.php?webName=$1 [L,QSA]
RewriteRule ^user/(.*)$ user.php?userID=$1 [L,QSA]
Make a difference between two regural expression:
If you would like to catch user ID you should filter for only numbers: \d
Then if there is another chars in url not only numbers you can match for anything except kslash: [^/]
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^user/(\d+)$ user.php?userID=$1 [L,QSA]
RewriteRule ^user/([^/]+)$ user.php?webName=$1 [L,QSA]

.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 change url stucture with htaccess

I have this url myurl.com/index.php?hello=folder/7f6c06
I want to change it to myurl.com/folder/7f6c06
I tried
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ index.php?hello=$1 [QSA,L]
is not working in godaddy hosting.
It doesn't work because your pattern ([^/]+) captures everything up to but not including the first /, but the URI you are looking to capture and pass into hello= includes a / itself. Since there are additional characters after the first / but the pattern matches at most one / optionally at the end, the pattern would never match as you have it.
Instead just use (.+)/? to capture everything up to an optional possible trailing slash.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/?$ index.php?hello=$1 [QSA,L]

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.

.htaccess url rewrite does not return entire URL

.htacess rules I've used -
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/\.]+)/?$ profile.php?user=$1 [L]
RewriteRule ^(.*)$ profile.php?user=$1 [QSA,NE]
correct example -
http://mydomain.com/http://fatalweb.com/articles/8/definitions-for-htaccess-regex-character
the value passed to $1 is - http://fatalweb.com/articles/8/definitions-for-htaccess-regex-character
http://mydomain.com/https://www.google.com/search?q=_get&ie=utf-8&oe=utf-8&aq=t
the value passed to $1 is - https:/www.google.com/search
How do i make sure the entire url https://www.google.com/search?q=_get&ie=utf-8&oe=utf-8&aq=t along with all the query parameters are passed to $1
In your question, is it https:/www.google.com/search? There is one slash after https:/ is missing.
Before you are going to use the empty file handler, you need to specify at least one rule. Don't know why, but that's how it is working. Check this out for your use case:
RewriteEngine On
RewriteRule ^([^/]*)\.dummy$ ./?dummy=$1&%{QUERY_STRING} [L]
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ ./%{QUERY_STRING}
The line RewriteRule ^([^/]*)\.dummy$ ./?dummy=$1&%{QUERY_STRING} [L] is dummy. Check and see if this works for you! :)

Categories