How change the pagination url in .htaccess - pagination

i am new to .htaccess an i want to know how to change the following url.
fun.php?search=&submit=search&page=2
Int to this
fun/page/2

I think it cannot be done in a direct way. Php GET method is needed to pass variables to the php code. However, you can add "fun" directory,add "page" directory, add "2" directory and add 2.php into your page directory with this code
<?php
header('Location:http://yourweb/fun.php?search=&submit=search&page=2');
?>
add a .htaccess file into "2" directory with this code
DirectoryIndex 2.php
That's all. Remember, php get method variable is not mean't to be shortened. Instead, use POST method
Hope it works

Related

HTACCESS - Get Path without filename

I was wondering how to get just the request path without the file name, i.e. the folder where the requested file is in. Is that possible with the HTACCESS file or is there a variable like% {REQUEST_URI} without the filename?
I would then like to deposit the code in the htaccess in the / directory. I want to find out the folder path so that I don't have to enter the path for each subfolder individually. So when I access example.com/folder the code checks the same as it does on example.com/folder2.
So I want to redirect TO a maintenance page FROM every URL in whose same folder there is a maintenance.txt.
I would then like to check with an If statement in the folder of the requested file whether a certain file ("maintenance.txt") exists. Because I want to "block" every folder where this file exists in it.
Maybe it is possible with RegEx? I thought of something like this:
<If "-f %{FOLDER_OF_REQUESTED_FILE}/maintenance.txt">
ErrorDocument 200 /assets/maintenance.html
</If>

execute script when directory listing by .htaccess

I have a http server (apache HTTPD v2.4) where directory listing is enabled. I configured it to use the 'fancy-index' from https://github.com/Vestride/fancy-index, and works great. However, its searching function didn't give what I was looking for.
I'd like to configure the webserver such that if I goto http://mywebserver.com/someDir/*?list, it will return a list of all the files in someDir in a "file, filesize" format.
Or, if I goto http://mywebserver.com/someDir/essay2020*.txt?list , I'll get a custom list of all essay2020*.txt files. Or some form of a simple regex: say "essay2020[0-2]*.txt".
I can write a php/perl/python script that can read a directory and return the 'file, filesize' list. But how do I configure the .htaccess to call this script? Do I have to muck around with 'RewriteRule'? or something special?
As an added note, I'm not limited to modifying .htaccess only. If I can do all of this by modifying the httpd.conf, I'm open to that suggestion too.
Thanks.
The answer in Comments by CBroe did it. By just modifying the apache2.conf file to add:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^list$
RewriteRule .* /search.php?path=%{REQUEST_URI}
within my <VirtualHost>, I'm now able to forward everything to search.php, where there I can parse and do what needs to be done. All w/o revealing search.php's existence. Thanks.

How to rewrite a url with id and title passed

Basically, what i want is this:
For example, I have a url that looks like this :
example.com/stories/show.php?id=$id
I want that url to look like this
example.com/stories/$id/$title
And I want the title to look like this:
I love stackoverflow to
I-love-stackoverflow
Seperating each words with a dash.
Am I supposed to pass the title along with the id??
e.g example.com/stories/show.php?id=$id&title=$title
And how do I achieve all this with .htaccess ??
A good example of what I want to do is used by stackoverflow.. Check the url of this question.
You only need some simple rule like in the below example
RewriteEngine on
# rewrite stories/<id>/<title> to show.php
RewriteCond %{REQUEST_URI} stories/([0-9]+)/([a-z\-A-Z]+)
RewriteRule (.*) show.php?id=%1&title=%2 [L]
Place the above in your .htaccess file, which should be located under the stories folder. Of course you also need to make sure so that the rewrite module is enabled on your server. This could be done by running the following commands from the terminal:
sudo a2enmod rewrite
sudo apachectl restart
Now you should be able to rewrite an url like:
http://example.com/stories/666/stack-overflow
to:
http://example.com/stories/show.php?id=666&title=stack-overflow
Then you could create your links in the following manner:
<?php
// php example
$title = "I love stackoverflow";
$id = 666;
?>
link
I think you should read up on how url rewriting really works, here is a couple of links that might be useful:
URL Rewriting for Beginners
URL Rewriting Guide

htaccess - creating directories and files of the same name

I want to create a bunch of files without an extension showing at the end. The easiest way to do that was to do this:
/usa/index.php
/usa/alaska/index.php
/usa/alabama/index.php
/usa/california/index.php
What I want to do is this
/usa/alaska.php
/usa/alabama.php
/usa/california.php
and have it show up as:
/usa/alaska
/usa/alabama
/usa/california
However, I have one more level I want to add to this, the cities
/usa/alaska/adak.php
/usa/alaska/anchorage.php
/usa/california/los-angles.php
I don't want the ".php" showing up, but then each state exists as both a file and a directory. What I want is an htaccess rule that serves up the file version of the file, not the directory which is the default. I also want to strip the .php off of the end of the files so the final result looks like
/usa
/usa/alaska (alaska.php)
/usa/alaska/adak (adak.php)
I know I can get close to this by creating all the directories and using index.php for each directory, but then I will have thousands of directories each with one file in it and updating is a pain in the butt. I would much rather have one directory with 1000 files in it, than 1000 directories with 1 file in it.
Please, can someone point me in the right direction and know that I am doing this for all 50 states.
Jim
I would also suggest using a single php (e.g. index.php) file and redirecting all urls starting with usa to it, instead of separating them in different directories and files. The you'd need a couple of rewrite rules like the following
RewriteEngine On
RewriteRule ^usa/([^/.]+)$ index.php?state=$1 [L]
RewriteRule ^usa/([^/]+)/([^/.]+)$ index.php?state=$1&city=$2 [L]
So then in your index.php you'd only need to check the $_GET parameters.
Update:
If you don't feel comfortable enough to use a database and pull the needed data from there you could always use the parameters to dynamically include/require the needed files. Something like this
<?php
$source = ''; //or the 'ROOT' directory
if(isset($_GET['state'])) $source .= $_GET['state'].'/';
if(isset($_GET['city'])) $source .= $_GET['city'].'.php';
include($source); // here $source would be something like 'alaska/adak.php'
// and is assumed that the dir 'alaska' is on the same
// level as 'index.php'
?>
But to answer your original question nevertheless you could use the following .htaccess
RewriteEngine On
RewriteRule ^usa/([^/.]+)$ usa/$1.php [L]
RewriteRule ^usa/([^/]+)/([^/.]+)$ usa/$1/$2.php [L]
what about creating just one single file:
/usa/index.php
With
$_SERVER["REQUEST_URI"]
you can read the current URI.
Well, now if a user enters "http://domain.foo/usa/alaska" for example, he will get an 404 error of course.
But to call your index.php instead, you could write this line to the .htaccess:
ErrorDocument 404 /usa/index.php
Now the index.php receives everything what is written to the URI and you can match the result and include files or handle errors.
But maybe there is a better solution with .htaccess only, don't know. :)

Redirect 301 cgi-bin/ to new URL

it's my first question here :)
I got a problem for redirecting URL:
I have old URL like www.domain.com/cgi-bin/category.cgi?type=...
And try to redirect them to www.domain.com on the htaccess
but I still have 404 error...
This is my rule :
RewriteRule ^cgi-bin/(.*)$ http://www.domain.com [R=301,L]
I verified if there are something in the conf about cgi-bin but nothing.
I did a test with "cgi-bin2" and it works...
So what can i do ?
I don't know where you problem come from but why don't you try to write a perl script which will redirect to your base domain url ?
(it can work if you have, for example, just few cgi files previously used).
In your example it seems you want to redirect "category.cgi".
so, in our case, write a "category.cgi" file in your "cgi-bin" folder and write this code inside it :
#!/usr/bin/perl
#
# fixedredir.cgi
use strict;
use warnings;
my $URL = "http://www.yourdomain.com/";
print "Status: 301 Moved\nLocation: $URL\n\n"
Hope it help !

Resources