lighttpd htaccess translation !-d !-f and losing get query - .htaccess

I currently try to translate a .htaccess file which is provided by the chyrp installation routine to lighttpd via the mod_rewrite module.
The source .htaccess file is as follows:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase {$index}
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.+$ index.php [L]
</IfModule>
Directory layout (webroot)
/chyrp/
/chyrp/includes/
/chyrp/admin/
/chyrp/feathers/
/chyrp/modules/
/chyrp/themes/
My current attempt
index-file.names = ( "index.php" )
url.access-deny = ( ".twig" )
#taking care of the directory, check if it is one of the known ones, if so go on
#if it is not, redirect to index
url.rewrite-once = (
"^/(|chyrp/)(admin|themes|feathers|modules|includes)(.*)$" => "/chyrp/$2$3",
"^/(|chyrp/).*/$" => "/chyrp/index.php"
)
#check if files exists, if not rewrite to a index.php in the same directory
url.rewrite-if-not-file = (
"^/(|chyrp/)(admin|themes|feathers|modules|includes)(/(index.php)?(.*))?$" => "/chyrp/$2/index.php$5",
"^/(|chyrp/)(.*)" => "/chyrp/index.php"
)
It mostly works, except for the search functionality (testable here: srctwig.com), which does not rewrite correctly (at least that's my guess) or somewhere the query gets lost.
The search routine itself works properly (demonstration search with 0 results)
A working demo of chyrp can be seen at chyrp demo on apache2
What am I doing wrong?

The reason for the issue was, I was not aware of having to dissect the url in order to extract the get query string which I needed to preserve according to a lighttpd mod_rewrite wiki entry
url.rewrite-once = (
"^/(|chyrp/)(admin|themes|feathers|modules|includes)/(.*)$" => "/chyrp/$2/$3",
"^/(|chyrp/).+/(([^\?]+)?(\?.+)?)$" => "/chyrp/index.php$2"
)
url.rewrite-if-not-file = (
"^/(|chyrp/)(admin|themes|feathers|modules|includes)(/([^\?]+)?(\?.+)?)?$" => "/chyrp/$2/index.php$5",
"^/(|chyrp/)(([^\?]+)?(\?.+)?)$" => "/chyrp/index.php$2",
"^/(|chyrp/).*$" => "/chyrp/index.php"
)
Dissect:
url.rewrite-once
"^/(|chyrp/)(admin|themes|feathers|modules|includes)/(.*)$" => "/chyrp/$2/$3",
These directories exist. Do not change anything except for ensuring the proper prefix if not alread there.
"^/(|chyrp/).+/(([^\?]+)?(\?.+)?)$" => "/chyrp/index.php$2"
Check if a non existant subdir was requested and check for ?, treat everything after that as php get query and append it to the rewrite target.
url.rewrite-if-not-file
"^/(|chyrp/)(admin|themes|feathers|modules|includes)(/([^\?]+)?(\?.+)?)?$" => "/chyrp/$2/index.php$5",
if the target does not exist just swap out the filename and keep the query (as above)
"^/(|chyrp/)(([^\?]+)?(\?.+)?)$" => "/chyrp/index.php$2",
go to the basefolder if none of the above matches and try to still pass the query
"^/(|chyrp/).*$" => "/chyrp/index.php"
this very last one is just for testing (put foobarfailure.php there to assert it never reaches that line)

Related

Using htaccess how do I RewriteRule/RewriteCond with no filename?

Hoping this isn't a duplicate, done a lot of looking and I just get more confused as I don't use .htaccess often.
I would like to have some pretty URLs and see lots of help regarding getting information where for example index.php is passed a parameter such as page. So I can currently convert www.example.com/index.php?page=help to www.example.com/help.
Obviously I'm not clued up on this but I would like to parse a URL such as www.example.com/?page=help.
Can't seem to find much info and adapting the original I am obviously going wrong somewhere.
Any help or pointers in the right direction would be greatly appreciated. I'm sure its probably stupidly simple.
My alterations so far which do not seem to work are:
RewriteCond %{THE_REQUEST} ^.*/?page=$1
RewriteRule ^(.*)/+page$ /$1[QSA,L]
Also recently tried QUERY_STRING but just getting server error.
RewriteCond %{QUERY_STRING} ^page=([a-zA-Z]*)
RewriteRule ^(.*) /$1 [QSA,L]
Given up as dead to the world so thought I would ask. Hoping to ensure the request/url etc starts ?page and wanting to make a clean URL from the page parameter.
This is the whole/basic process...
1. HTML Source
Make sure you are linking to the "pretty/canonical" URL in your HTML source. This should be a root-relative URL starting with a slash (or absolute), in case you rewrite from different URL path depths later. For example:
Help Page
2. Rewrite the "pretty" URL
In .htaccess (using mod_rewrite), internally rewrite the "pretty" URL back to the file that actually handles the request, ie. the "front-controller" (eg. index.php, passing the page URL parameter if you wish). For example:
DirectoryIndex index.php
RewriteEngine On
# Rewrite URL of the form "/help" to "index.php?page=help"
RewriteRule ^[^.]+$ index.php?page=$0 [L]
The RewriteRule pattern ^[^.]+$ matches any URL-path that does not include a dot. By excluding a dot we can easily omit any request that would map to a physical file (that includes a file extension delimited by a dot).
The $0 backreference contains the entire URL-path that is matched by the RewriteRule pattern.
The DirectoryIndex is required when the "homepage" (root-directory) is requested, when the URL-path is otherwise empty. In this case the page URL parameter is not passed to our script.
3. Implement the front-controller / router (ie. index.php)
In index.php (your "front-controller" / router) we read the page URL parameter and serve the appropriate content. For example:
<?php
$pages = [
'home' => '/content/homepage.php',
'help' => '/content/help-page.php',
'about' => '/content/about-page.php',
'404' => '/content/404.php',
];
// Default to "home" if "page" URL param is omitted or is empty
$page = empty($_GET['page']) ? 'home' : $_GET['page'];
// Default to 404 "page" if not found in the array/DB of pages
$handler = $pages[$page] ?? $pages['404'];
include($_SERVER['DOCUMENT_ROOT'].$handler);
As seen in the above script, the actual "content" is stored in the /content subdirectory. (This could also be a location outside of the document root.) By storing these files in a separate directory they can be easily protected from direct access.
4. Redirect the "old/ugly" URL to the "new/pretty" URL [OPTIONAL]
This is only strictly necessary (in order to preserve SEO) if you are changing an existing URL structure and the "old/ugly" (original) URLs have been exposed (indexed by search engines, linked to by third parties, etc.), otherwise the "old" URL (ie. /index.php?page=abc) is accessible. This is the same whenever you change an existing URL structure.
If the site is new and you are implementing the "new/pretty" URLs from the start then this is not so important, but it does prevent users from accessing the old URLs if they were ever exposed/guessed.
The following would go before the internal rewrite and after the RewriteEngine directive. For example:
# Redirect "old" URL of the form "/index.php?page=help" to "/help"
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{REQUEST_URI} ^/index\.php$ [OR]
RewriteCond %{QUERY_STRING} ^page=([^.&]*)
RewriteRule ^(index\.php)?$ /%1 [R=301,L]
The check against the REDIRECT_STATUS environment variable prevents a redirect-loop by not redirecting requests that have already been rewritten by the later rewrite.
The %1 backreference contains the value of the page URL parameter, as captured from the preceding CondPattern (RewriteCond directive). (Note how this is different to the $n backreference as used in the rewrite above.)
The above redirects all URL variants both with/without index.php and with/without the page URL parameter. For example:
/index.php?page=help -> /help
/?page=help -> /help
/index.php -> / (homepage)
/?page= -> / (homepage)
TIP: Test first with 302 (temporary) redirects to prevent potential caching issues.
Comments / improvements / Exercises for the reader
The above does not handle additional URL parameters. You can use the QSA (Query String Append) flag on the initial rewrite to append additional URL parameters on the initially requested URL. However, implementing the reverse redirect is not so trivial.
You don't need to pass the page URL parameter in the rewrite. The entire (original) URL is available in the PHP superglobal $_SERVER['REQUEST_URI'] (which also includes the query string - if any). You can then parse this variable to extract the required part of the URL instead of relying on the page URL parameter. This generally allows greatest flexibility, without having to modify .htaccess later.
However, being able to pass a page URL parameter can be "useful" if you ever want to manually rewrite (override) a URL route using .htaccess.
Incorporate regex (wildcard pattern matching) in the "router" script so you can generate URLs with "parameters". eg. /<page>/<param1>/<param2> like /photo/cat/large.
Reference:
https://httpd.apache.org/docs/2.4/rewrite/
https://httpd.apache.org/docs/2.4/rewrite/intro.html
https://httpd.apache.org/docs/2.4/mod/mod_rewrite.html
RewriteCond %{QUERY_STRING} ^page=([^&]+)
RewriteRule ^$ /%1? [R=302,L]
Can't delete and didn't want to waste anyones time responding.

HTACCESS Rewrite rule merging query parameters into 1st parameter

I am struggling to get the $_GET parameters to sit under their respective key.
I have the following HTACCESS rule:
RewriteRule ^view/new/(.*)/?(.*)/?(.*)/?(.*)$ vehicles/new-details.php?man=$1&model=$2&trim=$3&engine=$4 [QSA]
When accessing this page, I am temporarily printing out the $_GET array:
print_r($_GET);
This returns:
Array ( [man] => BMW/3-Series/SEL/EVO-3.0-TFSI [model] => [trim] =>
[engine] => )
Whereas it should return:
Array ( [man] => BMW [model] => 3-Series [trim] => SEL [engine] => EVO-3.0-TFSI)
The man parameter is required, whereas all others are optional.
Not sure what is happening here. Could it be the fact that I have made parameters optional in the rewrite rule?
RewriteRule ^view/new/(.*)/?(.*)/?(.*)/?(.*)$ vehicles/new-details.php?man=$1&model=$2&trim=$3&engine=$4 [QSA]
(.*) will take in all characters into each capture group, you want to qualify this to take in all characters up to the /.
So replace (.*) with ([^/]*) which is telling the mod_rewrite to "Take any character until you meet a /";
RewriteRule ^view/new/([^/]*)/?([^/]*)/?([^/]*)/?([^/]*)$ vehicles/new-details.php?man=$1&model=$2&trim=$3&engine=$4 [QSA]
This should give you the result you expect.
Edit: Read here for a far fuller answer!

how do i display the user nickname instead of the ID on the page profile [duplicate]

Normally, the practice or very old way of displaying some profile page is like this:
www.domain.com/profile.php?u=12345
where u=12345 is the user id.
In recent years, I found some website with very nice urls like:
www.domain.com/profile/12345
How do I do this in PHP?
Just as a wild guess, is it something to do with the .htaccess file? Can you give me more tips or some sample code on how to write the .htaccess file?
According to this article, you want a mod_rewrite (placed in an .htaccess file) rule that looks something like this:
RewriteEngine on
RewriteRule ^/news/([0-9]+)\.html /news.php?news_id=$1
And this maps requests from
/news.php?news_id=63
to
/news/63.html
Another possibility is doing it with forcetype, which forces anything down a particular path to use php to eval the content. So, in your .htaccess file, put the following:
<Files news>
ForceType application/x-httpd-php
</Files>
And then the index.php can take action based on the $_SERVER['PATH_INFO'] variable:
<?php
echo $_SERVER['PATH_INFO'];
// outputs '/63.html'
?>
I recently used the following in an application that is working well for my needs.
.htaccess
<IfModule mod_rewrite.c>
# enable rewrite engine
RewriteEngine On
# if requested url does not exist pass it as path info to index.php
RewriteRule ^$ index.php?/ [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php?/$1 [QSA,L]
</IfModule>
index.php
foreach (explode ("/", $_SERVER['REQUEST_URI']) as $part)
{
// Figure out what you want to do with the URL parts.
}
I try to explain this problem step by step in following example.
0) Question
I try to ask you like this :
i want to open page like facebook profile www.facebook.com/kaila.piyush
it get id from url and parse it to profile.php file and return featch data from database and show user to his profile
normally when we develope any website its link look like
www.website.com/profile.php?id=username
example.com/weblog/index.php?y=2000&m=11&d=23&id=5678
now we update with new style not rewrite we use www.website.com/username or example.com/weblog/2000/11/23/5678 as permalink
http://example.com/profile/userid (get a profile by the ID)
http://example.com/profile/username (get a profile by the username)
http://example.com/myprofile (get the profile of the currently logged-in user)
1) .htaccess
Create a .htaccess file in the root folder or update the existing one :
Options +FollowSymLinks
# Turn on the RewriteEngine
RewriteEngine On
# Rules
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php
What does that do ?
If the request is for a real directory or file (one that exists on the server), index.php isn't served, else every url is redirected to index.php.
2) index.php
Now, we want to know what action to trigger, so we need to read the URL :
In index.php :
// index.php
// This is necessary when index.php is not in the root folder, but in some subfolder...
// We compare $requestURL and $scriptName to remove the inappropriate values
$requestURI = explode(‘/’, $_SERVER[‘REQUEST_URI’]);
$scriptName = explode(‘/’,$_SERVER[‘SCRIPT_NAME’]);
for ($i= 0; $i < sizeof($scriptName); $i++)
{
if ($requestURI[$i] == $scriptName[$i])
{
unset($requestURI[$i]);
}
}
$command = array_values($requestURI);
With the url http://example.com/profile/19837, $command would contain :
$command = array(
[0] => 'profile',
[1] => 19837,
[2] => ,
)
Now, we have to dispatch the URLs. We add this in the index.php :
// index.php
require_once("profile.php"); // We need this file
switch($command[0])
{
case ‘profile’ :
// We run the profile function from the profile.php file.
profile($command([1]);
break;
case ‘myprofile’ :
// We run the myProfile function from the profile.php file.
myProfile();
break;
default:
// Wrong page ! You could also redirect to your custom 404 page.
echo "404 Error : wrong page.";
break;
}
2) profile.php
Now in the profile.php file, we should have something like this :
// profile.php
function profile($chars)
{
// We check if $chars is an Integer (ie. an ID) or a String (ie. a potential username)
if (is_int($chars)) {
$id = $chars;
// Do the SQL to get the $user from his ID
// ........
} else {
$username = mysqli_real_escape_string($char);
// Do the SQL to get the $user from his username
// ...........
}
// Render your view with the $user variable
// .........
}
function myProfile()
{
// Get the currently logged-in user ID from the session :
$id = ....
// Run the above function :
profile($id);
}
Simple way to do this. Try this code. Put code in your htaccess file:
Options +FollowSymLinks
RewriteEngine on
RewriteRule profile/(.*)/ profile.php?u=$1
RewriteRule profile/(.*) profile.php?u=$1
It will create this type pretty URL:
http://www.domain.com/profile/12345/
For more htaccess Pretty URL:http://www.webconfs.com/url-rewriting-tool.php
It's actually not PHP, it's apache using mod_rewrite. What happens is the person requests the link, www.example.com/profile/12345 and then apache chops it up using a rewrite rule making it look like this, www.example.com/profile.php?u=12345, to the server. You can find more here: Rewrite Guide
ModRewrite is not the only answer. You could also use Options +MultiViews in .htaccess and then check $_SERVER REQUEST_URI to find everything that is in URL.
There are lots of different ways to do this. One way is to use the RewriteRule techniques mentioned earlier to mask query string values.
One of the ways I really like is if you use the front controller pattern, you can also use urls like http://yoursite.com/index.php/path/to/your/page/here and parse the value of $_SERVER['REQUEST_URI'].
You can easily extract the /path/to/your/page/here bit with the following bit of code:
$route = substr($_SERVER['REQUEST_URI'], strlen($_SERVER['SCRIPT_NAME']));
From there, you can parse it however you please, but for pete's sake make sure you sanitise it ;)
It looks like you are talking about a RESTful webservice.
http://en.wikipedia.org/wiki/Representational_State_Transfer
The .htaccess file does rewrite all URIs to point to one controller, but that is more detailed then you want to get at this point. You may want to look at Recess
It's a RESTful framework all in PHP

Variable in .htaccess file

i want to redirect my pages whics are ending with the number.
for example:
www.example.com/content/1213 => www.example.com/content/sport/1213
thats ok; but i have pages, ending with the words too at the same folder ( www.example.com/content/xxxx )
.htaccess musnt do something to these pages.
i mean:
www.example.com/content/1213 => www.example.com/content/sport/1213 OK
www.example.com/content/xxx => DO NOTHING
Can i do it with htaccess?
You can put this code in your htaccess (which has to be in your document root folder)
RewriteEngine On
RewriteRule ^(content)/([0-9]+)$ /$1/sport/$2 [R=301,L]
Note: don't forget to enable mod_rewrite

remove directory after main url using htaccess

With the following url http://www.example.com/de/here/ I want to remove the "de" directory (or whatever may be in front of the "here" directory, if anything even is in front of it) so a user is directed to http://www.example.com/here/ instead, which is a directory that does actually exist.
The url could even be something like http://www.example.com/it/here/ or any other combination of 2 letters.
The url could also just be http://www.example.com/here/ in which case I don't want anything removed at all.
I have searched for a solution here but cant seem to make anything work correctly so any help would be much appreciated.
You can use this kind of htaccess :
RewriteEngine On
RewriteBase /
RewriteRule ^[A-Za-z]{2}/(.*)$ $1 [L,R=301]
Example of redirections caused by this code :
http://www.example.com/de/foo/ => http://www.example.com/foo/
http://www.example.com/de/ => http://www.example.com/
http://www.example.com/it/bar/ => http://www.example.com/bar/
http://www.example.com/FR/baz/ => http://www.example.com/baz/
Please note you won't be able to access the language (de, it, fr...) anymore.
Another point, be careful with this kind of url (the redirection will be executed twice) :
http://www.example.com/de/go/ => http://www.example.com/go/
http://www.example.com/go/ => http://www.example.com/
EDIT
Now I've got more details, here is an htaccess you can you to remove the language for specified folders :
RewriteEngine On
RewriteBase /
RewriteRule ^[A-Za-z]{2}/here/(.*)$ here/$1 [L,R=301]
RewriteRule ^[A-Za-z]{2}/anotherfolder/(.*)$ anotherfolder/$1 [L,R=301]

Resources