Jump to content

PHP and mod rewrite


Skitty
 Share

Recommended Posts

I've been working on this php script based off of tinyib to work just like 4chan, yeah.. you heard right.

I came to a problem when I noticed how 4chan interprets page requests as so~

<a href="1">1</a>

Now the above is obviously a request for the second page on almost any board at the place and when you click it, the url changes from ~

http://boards.4chan.org/g/

to the url

http://boards.4chan.org/g/1

And if you add a trailing backslash to the url as so~

http://boards.4chan.org/g/1/

you will get an error which should mean the request is not a directory but an argument sent over the url like the typical forum values you see like ~

forum.net/forum.php?Page=1

Some one told me that moot uses mod_rewrite to interpret the request, I couldn't get the said module to work under uniserver for some reason, even though "server-info" screams at me that the module is loaded and ready to go, but when I add a rule, it gives me a server error or apache fails to run when restarted.

I'm basically very close to replicating 4chans script. but I cannot figure out how to interpret the url in my script so I can deliver the right page.

Heres how 4chan uses the page ref ~

<a href="1">1</a>

Very simple looking but I don't know how to catch this and not get a 404 error because of the "1" which apache thinks is a directory.

Edited by THAT1ANONYMOUSEDUDE
Link to comment
Share on other sites

Loaded items

Loadfile "C:/UniServer/usr/local/php/ssleay32.dll"
Loadfile "C:/UniServer/usr/local/php/libeay32.dll"
Loadfile "C:/UniServer/usr/local/php/libmysql.dll"
#Loadfile "C:/UniServer/usr/local/php/php5ts.dll"
LoadModule actions_module modules/mod_actions.so
LoadModule alias_module modules/mod_alias.so
LoadModule asis_module modules/mod_asis.so
LoadModule auth_basic_module modules/mod_auth_basic.so
#LoadModule auth_digest_module modules/mod_auth_digest.so
#LoadModule authn_alias_module modules/mod_authn_alias.so
#LoadModule authn_anon_module modules/mod_authn_anon.so
#LoadModule authn_dbd_module modules/mod_authn_dbd.so
#LoadModule authn_dbm_module modules/mod_authn_dbm.so
LoadModule authn_default_module modules/mod_authn_default.so
LoadModule authn_file_module modules/mod_authn_file.so
#LoadModule authnz_ldap_module modules/mod_authnz_ldap.so
#LoadModule authz_dbm_module modules/mod_authz_dbm.so
LoadModule authz_default_module modules/mod_authz_default.so
LoadModule authz_groupfile_module modules/mod_authz_groupfile.so
LoadModule authz_host_module modules/mod_authz_host.so
#LoadModule authz_owner_module modules/mod_authz_owner.so
LoadModule authz_user_module modules/mod_authz_user.so
LoadModule autoindex_module modules/mod_autoindex.so
#LoadModule cache_module modules/mod_cache.so
#LoadModule cern_meta_module modules/mod_cern_meta.so
LoadModule cgi_module modules/mod_cgi.so
#LoadModule charset_lite_module modules/mod_charset_lite.so
LoadModule dav_module modules/mod_dav.so
#LoadModule dav_fs_module modules/mod_dav_fs.so
#LoadModule dav_lock_module modules/mod_dav_lock.so
#LoadModule dbd_module modules/mod_dbd.so
LoadModule deflate_module modules/mod_deflate.so
LoadModule dir_module modules/mod_dir.so
#LoadModule disk_cache_module modules/mod_disk_cache.so
#LoadModule dumpio_module modules/mod_dumpio.so
LoadModule env_module modules/mod_env.so
#LoadModule expires_module modules/mod_expires.so
#LoadModule ext_filter_module modules/mod_ext_filter.so
#LoadModule file_cache_module modules/mod_file_cache.so
#LoadModule filter_module modules/mod_filter.so
LoadModule headers_module modules/mod_headers.so
#LoadModule ident_module modules/mod_ident.so
#LoadModule imagemap_module modules/mod_imagemap.so
LoadModule include_module modules/mod_include.so
LoadModule info_module modules/mod_info.so
LoadModule isapi_module modules/mod_isapi.so
#LoadModule ldap_module modules/mod_ldap.so
#LoadModule logio_module modules/mod_logio.so
LoadModule log_config_module modules/mod_log_config.so
#LoadModule log_forensic_module modules/mod_log_forensic.so
#LoadModule mem_cache_module modules/mod_mem_cache.so
LoadModule mime_module modules/mod_mime.so
#LoadModule mime_magic_module modules/mod_mime_magic.so
LoadModule negotiation_module modules/mod_negotiation.so
#LoadModule proxy_module modules/mod_proxy.so
#LoadModule proxy_ajp_module modules/mod_proxy_ajp.so
#LoadModule proxy_balancer_module modules/mod_proxy_balancer.so
#LoadModule proxy_connect_module modules/mod_proxy_connect.so
#LoadModule proxy_ftp_module modules/mod_proxy_ftp.so
#LoadModule proxy_http_module modules/mod_proxy_http.so
#LoadModule proxy_scgi_module modules/mod_proxy_scgi.so
#LoadModule reqtimeout_module modules/mod_reqtimeout.so
LoadModule rewrite_module modules/mod_rewrite.so
LoadModule setenvif_module modules/mod_setenvif.so
#LoadModule speling_module modules/mod_speling.so
LoadModule ssl_module modules/mod_ssl.so
LoadModule status_module modules/mod_status.so
#LoadModule substitute_module modules/mod_substitute.so
#LoadModule unique_id_module modules/mod_unique_id.so
LoadModule userdir_module modules/mod_userdir.so
#LoadModule usertrack_module modules/mod_usertrack.so
LoadModule version_module modules/mod_version.so
LoadModule vhost_alias_module modules/mod_vhost_alias.so

Virtual Host

<VirtualHost *:80 >
RewriteEngine On
RewriteOptions Inherit
  ServerName boards.phpchan.tk
  DocumentRoot C:/UniServer/www/chan
</VirtualHost>

Testing rewrite conditions I've used and do not work in neither .htaccess or httpd.conf

DirectoryIndex index.html
addtype application/x-httpd-php .php .html
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

the errors I get are usually 404's and 401's, I believe I was getting a 404 due to a bad alias condition I had in httpd.conf, as for the 401's, I do not have anything blocked so I can't see why I was getting that.

On a side note, I even tested this with

RewriteBase   /test
RewriteRule   ^index\.html$  1

just to see if it was working, obviously nothing is working with mod rewrites or I am failing to understand how to do this properly, I've already almost religiously followed every tutorial I've encountered on the web and still can't get this right.

Link to comment
Share on other sites

This was placed inside a VirtualHost section:

RewriteEngine On
RewriteRule ^/index\.shtml /test2.html
RewriteRule ^/$ /test2.html

It replaced both http://domain/ and http://domain/index.shtml with http://domain/test2.html. I imagine what you need is:

RewriteEngine On
RewriteRule ^/g/$ /g/1
Link to comment
Share on other sites

Thanks, I noticed the message was actually saying there was an internal error, the error info I gave earlier was incorrect, at the last minute, I actually noticed what was going on.

lol, silly me.

So I checked in the apache error log and found this nifty message that was logged more than a few thousand times and started getting logged around the same time I started trying this (yesterday), the massage ~

[error] [client 70.124.221.65] Options FollowSymLinks or SymLinksIfOwnerMatch is off which implies that RewriteRule directive is forbidden: C:/UniServer/www/test.php

So I threw this into .htaccess

<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^b$ test.php [L]
</IfModule>

And viola, moral of story; if there are error log files, f***ing use them.

Thank you for the assistance Valik.

Edit: But the main point of this thread remains unsolved, how do I catch the "1" value in the php script when its requested in this manner?

Subsequent Edit: Never mined, I've think I got this.

"modrewrite"

<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteBase   /
RewriteRule ^test/(.*)$ test.php
</IfModule>

Example php usage

<?php
$T=explode('/',$_SERVER["REQUEST_URI"],3);
$X=$T{2};
$Y = $X + 1;
echo'<a href="'.$Y.'">'.$Y.'</a>'
?>

Edited by THAT1ANONYMOUSEDUDE
Link to comment
Share on other sites

Subsequent Edit: Never mined, I've think I got this.

"modrewrite"

<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteBase   /
RewriteRule ^test/(.*)$ test.php
</IfModule>

Example php usage

<?php
$T=explode('/',$_SERVER["REQUEST_URI"],3);
$X=$T{2};
$Y = $X + 1;
echo'<a href="'.$Y.'">'.$Y.'</a>'
?>

the above was a crude way of doing it.

The below is what I needed.

RewriteRule ^([a-zA-Z0-9])/?$ test.php?board=$1&page=$2
RewriteRule ^([a-zA-Z0-9])/([0-9])/?$ test.php?board=$1&page=$2
RewriteRule ^([a-zA-Z0-9])/([0-1][0-5])/?$ test.php?board=$1&page=$2
 
RewriteRule ^([a-zA-Z0-9][a-zA-Z0-9])/?$ test.php?board=$1&page=$2
RewriteRule ^([a-zA-Z0-9][a-zA-Z0-9])/([0-9])/?$ test.php?board=$1&page=$2
RewriteRule ^([a-zA-Z0-9][a-zA-Z0-9])/([0-1][0-5])/?$ test.php?board=$1&page=$2
 
RewriteRule ^([a-zA-Z0-9][a-zA-Z0-9][a-zA-Z0-9])/?$ test.php?board=$1&page=$2
RewriteRule ^([a-zA-Z0-9][a-zA-Z0-9][a-zA-Z0-9])/([0-9])/?$ test.php?board=$1&page=$2
RewriteRule ^([a-zA-Z0-9][a-zA-Z0-9][a-zA-Z0-9])/([0-1][0-5])/?$ test.php?board=$1&page=$2
 
RewriteRule ^([a-zA-Z0-9][a-zA-Z0-9][a-zA-Z0-9][a-zA-Z0-9])/?$ test.php?board=$1&page=$2
RewriteRule ^([a-zA-Z0-9][a-zA-Z0-9][a-zA-Z0-9][a-zA-Z0-9])/([0-9])/?$ test.php?board=$1&page=$2
RewriteRule ^([a-zA-Z0-9][a-zA-Z0-9][a-zA-Z0-9][a-zA-Z0-9])/([0-1][0-5])/?$ test.php?board=$1&page=$2

<html>
   <head>
      <title>mod_rewrite test</title>
   </head>
   <body>
      <p>
         The requested board is:
         <?php echo $_GET['board']; ?>
      </p>
<p>
         The requested page is:
         <?php echo $_GET['page']; ?>
</p>
<?php $X=$_GET['page'];$Y = $X + 1;echo'<a href="'.$Y.'">'.$Y.'</a>' ?>
   </body>
</html>

Although, I am still having a problem figuring out a legitimate easy way to fix up the url in the users browser, lets say we were at "thisdomain.com" and we requested board "x", the url will look like "thisdomain.com/x".

How can I have the url be set as "thisdomain.com/x/", with the appended forward slash?

If you go to "boards.4chan.org/g" the url is set as "boards.4chan.org/g/", automatically appending the forward slash, the guy running that place sure used a whole boat load of mod rewrite tricks or something.

How would I go about adding that forward slash if its missing? I.e If you use the above example php and mod rewrite code and access the script w/o appending the slash mark and you click on the number link, the number is appended to the letter of the board requested, how do I stop that?

Edit: More specifically, I'm currently attempting to figure out how to append the trailing slash to the requested board, and if a board is requested along with the page number like so "domain.com/x/1/", give a 404, which shouldn't be to hard.

Edit #2: Obviously, I don't think mod rewrite is what I'm looking for in order to append the trailing slash on the client side, I guess I'll have to jumble up some php that echo's a meta refresh tag that fixes the url.

Edited by THAT1ANONYMOUSEDUDE
Link to comment
Share on other sites

  • 3 weeks later...

I've been working on this php script based off of tinybb to work just like 4chan, yeah.. you heard right.

I came to a problem when I noticed how 4chan interprets page requests as so~

<a href="1">1</a>

Now the above is obviously a request for the second page on almost any board at the place and when you click it, the url changes from ~

http://boards.4chan.org/g/

to the url

http://boards.4chan.org/g/1

And if you add a trailing backslash to the url as so~

http://boards.4chan.org/g/1/

you will get an error which should mean the request is not a directory but an argument sent over the url like the typical forum values you see like ~

forum.net/forum.php?Page=1

Some one told me that moot uses mod_rewrite to interpret the request, I couldn't get the said module to work under uniserver for some reason, even though "server-info" screams at me that the module is loaded and ready to go, but when I add a rule, it gives me a server error or apache fails to run when restarted.

I'm basically very close to replicating 4chans script. but I cannot figure out how to interpret the url in my script so I can deliver the right page.

Heres how 4chan uses the page ref ~

<a href="1">1</a>

Very simple looking but I don't know how to catch this and not get a 404 error because of the "1" which apache thinks is a directory.

What you need to do is the following:

<IfModule mod_rewrite.c>
#Turn on the engine
RewriteEngine on
#Set the base
RewriteBase (Set your base here)
 
RewriteRule ^\/g\/([0-9]+)\/?$ forum\.php?Page=$1 [L]
#First group is searching for what page the user is on
#Second group is where this request will be directed to on the directory
#[L] stops any more rules from executing
</IfModule>

Hope this helps!

Shane

Edited by shanet

[font="Comic Sans MS"]My code does not have bugs! It just develops random features.[/font]My Projects[list][*]Live Streaming (Not my project, but my edited version)[right]AutoIt Wrappers![/right][/list]Pure randomness[list][*]Small Minds.......................................................................................................[size="1"]Simple progress bar that changes direction at either sides.[/size][*]ChristmasIt AutoIt Christmas Theme..........................................................[size="1"]I WAS BOOOORED![/size][*]DriveToy..............................................................................................................[size="1"]Simple joke script. Trick your friends into thinking their computer drive is haywire![/size][/list]In Development[list][*]Your Background Task Organiser[*]AInstall Second Generation[/list]BEFORE POSTING ON THE FORUMS, TRY THIS:

%programfiles%/AutoIt3/autoit3.chm
Link to comment
Share on other sites

Hope this helps!

Shane

Sure did, thanks.

I can't believe I overlooked the [L] parameter, it was something I needed.

I also need to mention that It was my mistake saying this is based off of tinybb, when really it was tinyib...

Link to comment
Share on other sites

By the way, the parameter QSA is also useful. This allows to use GET-parameters in the URL, too. Example:

example.com/g/1?order=y

without QSA: example.com/forum.php?Page=1

with QSA: example.com/forum.php?order=y&Page=1

Edited by ProgAndy

*GERMAN* [note: you are not allowed to remove author / modified info from my UDFs]My UDFs:[_SetImageBinaryToCtrl] [_TaskDialog] [AutoItObject] [Animated GIF (GDI+)] [ClipPut for Image] [FreeImage] [GDI32 UDFs] [GDIPlus Progressbar] [Hotkey-Selector] [Multiline Inputbox] [MySQL without ODBC] [RichEdit UDFs] [SpeechAPI Example] [WinHTTP]UDFs included in AutoIt: FTP_Ex (as FTPEx), _WinAPI_SetLayeredWindowAttributes

Link to comment
Share on other sites

Oh yes I completely overlooked QSA ;)

Thanks ProgAndy :)

For the OP, QSA stands for Query String Append.

The flag could/should be changed to [QSA,L]

Glad to hear that it helped :graduated:

[font="Comic Sans MS"]My code does not have bugs! It just develops random features.[/font]My Projects[list][*]Live Streaming (Not my project, but my edited version)[right]AutoIt Wrappers![/right][/list]Pure randomness[list][*]Small Minds.......................................................................................................[size="1"]Simple progress bar that changes direction at either sides.[/size][*]ChristmasIt AutoIt Christmas Theme..........................................................[size="1"]I WAS BOOOORED![/size][*]DriveToy..............................................................................................................[size="1"]Simple joke script. Trick your friends into thinking their computer drive is haywire![/size][/list]In Development[list][*]Your Background Task Organiser[*]AInstall Second Generation[/list]BEFORE POSTING ON THE FORUMS, TRY THIS:

%programfiles%/AutoIt3/autoit3.chm
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...