Optional trailing slash mod_rewrite - apache

I have a very specific problem.
The title gives a lot of other questions that have correct answers, however I don't know how to fit this to my own rewrite rules.
My .htaccess looks like so:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^[^/]*/?$ index.php [NC,L]
RewriteCond %{REQUEST_URI} !(^/dev/zeroyear/$)
RewriteRule ^dev/zeroyear/([^/]+)/?([^/]*)/?([^/]*)/?([^/]*)/?$ /dev/zeroyear/index.php?var1=$1&var2=$2&var3=$3&var4=$4 [L,QSA]
I want the following 2 URLs to redirect to the same page:
http://timesheep.name/dev/zeroyear/news
http://timesheep.name/dev/zeroyear/news/
The last rewrite rule is causing a 500 because of the question mark I put, but the logs say nothing. If I change it to this:
RewriteRule ^dev/zeroyear/([^/]+)/([^/]*)/?([^/]*)/?([^/]*)/?$ /dev/zeroyear/index.php?var1=$1&var2=$2&var3=$3&var4=$4 [L,QSA]
The error goes away, but http://timesheep.name/dev/zeroyear/news will return a 404.
What can I do about this?

You've got a mix of non-greedy matches (which will eat up optional /'s) and optional /'s all mixed together. Try separating them into distinct matches:
RewriteCond %{REQUEST_URI} !(^/dev/zeroyear/$)
RewriteRule ^dev/zeroyear/([^/]+)/([^/]+)/([^/]+)/([^/]+?)/?$ /dev/zeroyear/index.php?var1=$1&var2=$2&var3=$3&var4=$4 [L,QSA]
RewriteCond %{REQUEST_URI} !(^/dev/zeroyear/$)
RewriteRule ^dev/zeroyear/([^/]+)/([^/]+)/([^/]+?)/?$ /dev/zeroyear/index.php?var1=$1&var2=$2&var3=$3 [L,QSA]
RewriteCond %{REQUEST_URI} !(^/dev/zeroyear/$)
RewriteRule ^dev/zeroyear/([^/]+)/([^/]+?)/?$ /dev/zeroyear/index.php?var1=$1&var2=$2 [L,QSA]
RewriteCond %{REQUEST_URI} !(^/dev/zeroyear/$)
RewriteRule ^dev/zeroyear/([^/]+?)/?$ /dev/zeroyear/index.php?var1=$1 [L,QSA]

Related

RewriteRule - remove params keys and keep values in address bar

RewriteEngine ON
RewriteRule ^video$ video.php [L]
the above line works
example.com/video is interpret as example.com/video.php
now I need example.com/video?id=5&s=lorem-ipsum
to interpret as example.com/video/5/lorem-ipsum - and vice versa
RewriteCond %{THE_REQUEST} video?id=([^\s&]+)&s=([^\s&]+) [NC]
RewriteRule ^ %1/%2? [R=301,L]
RewriteRule ^([\w-]+)/([\w-]+)/?$ video?id=$1&s=$2 [L,QSA]
RewriteRule ^([\w-]+)/([\w-]+)/?$ video?/$1/$2 [L,QSA]
doesn't work - in both directions
pls help
With your shown attempts, please try following .htaccess rules file. These rules are assuming you want to infernally rewrite to index.php you can change php file name in 2nd set of rules as per your requirement.
Make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
##External redirect Rules from here..
##Redirect 301 to example.com/video/5/lorem-ipsum from example.com/video?id=5&s=lorem-ipsum
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{THE_REQUEST} \s/(video)\?id=([^&]*)&s=(\S+)\s [NC]
RewriteRule ^ /%1/%2/%3? [R=301,L]
##Internal rewrite for example.com/video?id=5&s=lorem-ipsum
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^video/([^/]*)/(.*)/?$ index.php?id=$1&s=$2 [QSA,NC,L]

Rewrite condition to match ANY file extension

I have the following htaccess rule:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ $1/ [L,R=301]
What this does is insert a trailing slash character to all my URLs, i.e:
http://localhost/advertising becomes http://localhost/advertising/
Now I have some URLs such as: http://localhost/contact.html
But my htaccess rule is applying the slash to these URLs too - so the above URL becomes http://localhost/contact.html/
Now I added the following condition to prevent this happening:
RewriteCond %{REQUEST_URI} !\.(html|php)$
But I want to be a bit more clever than this - I want a single expression that will match ANY extension the user types in. How can I achieve this?
This will rewrite all directories without trailing slash!
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)(\..{3,5}|/)$
RewriteRule ^(.*)$ $1/ [L,R=301]

Apache mod_rewrite alias a path to another path

I have a weird rewriting problem due to some Drupal issue. I know not how to fix the Drupal issue, so this will be a temporary fix:
The path /the%20campanile/([0-9]+)/([0-9]+)/ (preferably with slash optional) should be an alias for /campanile/([0-9]+)/([0-9]+)/.
I've tried:
RewriteBase /
RewriteRule ^campy/([0-9]+)/([0-9]+)/$ campanile/$1/$2/
except that fails with 404 (presumably rewriting wrong). I've also tried with wildcards (*), but to no avail. I looked through about 16 posts on StackOverflow and a few other forums.
The entire .htaccess looks like this:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [QSA]
RewriteRule ^campy/([0-9]+)/([0-9]+)/$ campanile/$1/$2/
RewriteCond %{HTTP_HOST} ^danger-\.palyvoice\.com
RewriteRule ^(.*)$ /category/blogs/danger-zone [L]
What am I doing wrong?
Not sure why you're regular expression is ^campy/([0-9]+)/([0-9]+)/$ when you're trying to match something like /the%20campanile/([0-9]+)/([0-9]+)/. Groupings aside, the campy will never match the%20campanile.
You need to keep in mind that the URI is decoded before being processed through mod_rewrite, so you don't want to match against %20, but just the space:
RewriteRule ^/?the\ campanile/([0-9]+)/([0-9]+)/?$ campanile/$1/$2/ [L]
You also want these rules before your index.php routing rule, since the routing rule will rewrite it before it gets to your other rules:
RewriteEngine on
RewriteBase /
RewriteRule ^/?the\ campanile/([0-9]+)/([0-9]+)/?$ campanile/$1/$2/ [L]
RewriteCond %{HTTP_HOST} ^danger-\.palyvoice\.com
RewriteRule ^(.*)$ /category/blogs/danger-zone [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [QSA]

Apache 2.2 mod rewrite loop. How to replace [END]

RewriteEngine on
I have following code:
RewriteCond %{REQUEST_URL} ^$
RewriteRule ^files/(.*)$ getfile.php?file=$1 [L]
RewriteRule ^(.+)/index([0-9]*).html$ index.php?path=$1/&page=$2 [QSA]
RewriteRule ^(.+).html$ index.php?path=$1.html [QSA]
RewriteRule ^(.+)/(.+).htm$ index.php?path=$1/&article=$2.htm [QSA]
RewriteRule ^(.+/)$ index.php?path=$1 [QSA]
RewriteRule ^(.+)$ index.php?path=$1 [QSA]
This code translate urls to parameter path and article according to situation. All good and bad url request is routed to index.php except for files within folder files/.
I have problem with last line of code. It is primarily for all bad urls (all other is processed earlier). With this rule it start to loop over and over again, because all of url match this rule. This rule have to match once or never, but without [END] I cannot contrive. I have older version of apache (2.2) so I cannot use this direction.
Solution for this I have tried is something like this:
RewriteCond %{QUERY_STRING} !^.*path=([^&]*).*$
RewriteCond %{REQUEST_URI} !^/((files|design)/.+)$
RewriteRule ^(.+)$ index.php?path=$1 [QSA]
but cannot succeed.
Try adding a L flag to the end of each rule and add a redirect environment check in the beginning to pass through any previously internally redirected URI:
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]
RewriteRule ^files/(.*)$ getfile.php?file=$1 [L]
RewriteRule ^(.+)/index([0-9]*).html$ index.php?path=$1/&page=$2 [L,QSA]
RewriteRule ^(.+).html$ index.php?path=$1.html [L,QSA]
RewriteRule ^(.+)/(.+).htm$ index.php?path=$1/&article=$2.htm [L,QSA]
RewriteRule ^(.+/)$ index.php?path=$1 [L,QSA]
RewriteRule ^(.+)$ index.php?path=$1 [L,QSA]

htaccess rewrite

I am working on a project and it utilizes a shortened url for redirection, I've gotten the following code to work perfectly:
XBitHack Off
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{REQUEST_URI} \/([0-9A-Za-z]{4})$ [NC]
RewriteRule ^(.*) ./getdeck.php?deck=%1 [L]
Now I want to add a second redirect rule for /a/([0-9A-Za-z]{4} but no matter how I write it, the new rule fails. Can anyone help explain what I'm missing?
Examples I have tried:
RewriteCond %{REQUEST_URI} ^/a/.*$ [NC]
RewriteRule ^(.*) ./deck/makedeck.php?deck=%1 [L]
or
RewriteCond %{REQUEST_URI} ^/a/([0-9A-Za-z]{4})$ [NC]
RewriteRule ^(.*) ./deck/makedeck.php?deck=%1 [L]
You might have to repeat the first two conditions.