Member Avatar for diafol

Hi folks, I know this ain't php, but seemed the best place to put it for now.

Am trying to mod_rewrite the following:

http://www.nu.wetwork.org.uk/index.php?adran=first&tud=second
to
http://www.nu.wetwork.org.uk/first/second/

I'm a complete idiot when it comes to regex/apache, so any advice would be welcome. I have already managed to place 2 rules which:

1) renames nu.wetwork.org.uk to www.nu.wetwork.org.uk
2) gets rid of www.nu.wetwork.org.uk/index.php (and the index.html) to www.nu.wetwork.org.uk/

The second works OK as long as there is no querystring. Here's my ham-fisted attempt so far:

RewriteEngine On 
RewriteCond %{HTTP_HOST} ^nu.wetwork\.org\.uk
RewriteRule ^(.*)$ http://www.nu.wetwork.org.uk/$1 [R=301,L]

RewriteCond %{THE_REQUEST} ^GET\ .*/index\.(php|html)\ HTTP
RewriteRule ^(.*)index\.(php|html)$ /$1 [R=301,L]

In my test site, each page is referenced via index.php and querystring (always the 2 parameters). The faux-folders are just to give the site some structure / 'bookmarkability' for the user.

Thanks,
Ben.

Try modifying along the lines of this

RewriteEngine On 
RewriteCond %{HTTP_HOST} ^nu.wetwork\.org\.uk
RewriteRule ^(.*)$ http://www.nu.wetwork.org.uk/$1 [R=301,L]

RewriteRule ^/?(.*)/(.*)/?$ /index.php?adran=$1&tud=$2
Member Avatar for diafol

Thanks for the reply cwarn, unfortuntely that changes faux folders to querystring - I need the opposite. I just can't work out the syntax required to change

/index.php?adran=$1&tud=$2

to

/$1/$2/

I believe that is the correct syntax however the files should be accessible with AND without rewritten urls. Also if you want to make sure apache doesn't get into an infinit loop causing a 500 error then you could use the following:

RewriteEngine On 
RewriteCond %{HTTP_HOST} ^nu.wetwork\.org\.uk
RewriteRule ^(.*)$ http://www.nu.wetwork.org.uk/$1 [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?(.*)/(.*)/?$ /index.php?adran=$1&tud=$2

So to explain what those last three lines do, the two lines near the end beginning with RewriteCond% check that it is not rewriting a file or directory. Then the last line matches the regex on the left in the url and if that matches it views the page on the right similar to the preg_replace syntax. Is that what your after because you may need to just try viewing it in the new format and linking to the new format as both ways exist when using mod_rewrite?

commented: Brilliant! thank you very much - I was going off my rocker! +5
Member Avatar for diafol

cwarn - you are an absolute genius sir!
Thank you very much - worked like a charm!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.