Mapping URLs with mod rewrite - regex

I migrated my site to a new platform & I want to map old URLs like this:
anecdotage.com/index.php?aid=13337
to this:
anecdotage.com/articles/13336/
[Note the offset of one in the IDs]
Can I do this in apache's mod_rewrite. Any help appreciated!

You can't do any math with mod_rewrite by itself, meaning you can match against 13337 and do something to it to change it to 13336 without employing some kind of `RewriteMap to externally subtract a number by 1, but this requires access to either server or vhost config.
So in the vhost/server config you have to setup the map:
RewriteMap subtract prg:/path/to/script/that/subtracts-by-one.sh
This subtracts-by-one.sh script takes a number as an input, subtracts 1 from it, then prints out the result. Then to use it in a rewrite rule:
# to make sure we aren't clobbering legit requests
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} aid=([0-9]+)
RewriteRule ^/?index.php$ /articles/${subtract:%1}/? [L]
If you don't have access to rewrite maps, then you'll need to do it the hard way and enumerate all of your rewrites:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} aid=12345
RewriteRule ^/?index.php$ /articles/12344/? [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} aid=13337
RewriteRule ^/?index.php$ /articles/13336/? [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} aid=19911
RewriteRule ^/?index.php$ /articles/19910/? [L]
etc.

Related

HTACCESS RewriteCond and RewriteRule grouping

I have a htaccess file which redirects all files to the root index.php file unless certain conditions are met.
The following code works perfectly and is used to provide placeholder images for uploaded media assets if the website is run locally. This is to not need to download all files from the website so that the repository only consists of files.
The files that the following rules match look like:
[\d]{1,}.(thumbnail|medium|large).[\w]+
What I want to do is match the middle group and use it in the redirect rule but cannot seem to get it to work with:
RewriteCond %{REQUEST_URI} data/applications/.*/media/image/.*/[\d]+\.(.*)\.[\w]+$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .*$ data/applications/app/www/img/temp.$1.jpg [L]
The original code is here:
RewriteCond %{REQUEST_URI} data/applications/.*/media/image/.*/[\d]+\.thumbnail\.[\w]+$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .*$ data/applications/app/www/img/temp.thumbnail.jpg [L]
RewriteCond %{REQUEST_URI} data/applications/.*/media/image/.*/[\d]+\.medium\.[\w]+$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .*$ data/applications/app/www/img/temp.medium.jpg [L]
RewriteCond %{REQUEST_URI} data/applications/.*/media/image/.*/[\d]+\.large\.[\w]+$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .*$ data/applications/app/www/img/temp.large.jpg [L]
How can i combine the 3 separate matches into one rule?
You can make a single rules as this:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(data/applications)/.*/media/image/.*/\d+\.([^.]+)\.\w+$ $1/app/www/img/temp.$2.jpg [L]

Keep getting 500 Internal Server Error URL Rewrite

I have something like the following page:
www.domain.com/games/?puzzle=value
I'd like for it to show up as
www.domain.com/games/value
Here is the rewrite I wrote for forwarding to the original domain that causes a 500 Internal Server Error:
#forward games/value to games/?puzzle=value
RewriteRule ^games/(.+?)/?$ /games/index.php?puzzle=$1 [L,QSA]
Would someone help me solve my issue, I'm really bad at rewrites.
EDIT:
I'm currently using the following to allow parameters from foo on my main page.
# Internally forward /value to /index.php?foo=value
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)/?$ /index.php?foo=$1 [L,QSA]
If I remove that then the following will work for what I want, but I need both instances to work.
#forward games/value to games/?puzzle=value
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^games/(.+?)/?$ /games/index.php?puzzle=$1 [L,QSA]
That is due to looping since URL after rewrite is also matching your pattern ^games/(.+?)/?$.
You need to exclude real files/directories from this rewrite rule:
#forward games/value to games/?puzzle=value
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^games/(.+?)/?$ /games/index.php?puzzle=$1 [L,QSA]
# Internally forward /value to /index.php?foo=value
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)/?$ /index.php?foo=$1 [L,QSA]

rewrite move up directory

I have a rewrite rule where if you type:
www.whatever.com/profile-one.php
that is the url you see, but the actual template page is:
www.whatever.com/profile.php
Here are the lines in htaccess:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)([^/])$ http://www.whatever.com/$1$2/ [R=301,L]
RewriteRule profile-(.*)\.php$ profile.php?name=$1
My problem is, if you visit a url like:
www.whatever.com/second/profile-one.php
It still tries to bring back the profile page, even though the profile.php template does not exist in the "second" directory. What would be the best way to prevent this please?
This is happening due to wrong regex here. You need to use start anchor in your 2nd rule:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)([^/])$ http://www.whatever.com/$1$2/ [R=301,L]
RewriteRule ^profile-(.*)\.php$ profile.php?name=$1 [L,QSA]

mod rewrite condition fix to avoid partial word match

Its probably very simple, but I can't make it work =/
I have this rule:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^admin/?(.+?)?$ adm.php?route=$1&%{QUERY_STRING} [L]
Well, its suppose to send the user to the adm.php when there is a localhost/admin or localhost/admin/anything url, preserving the query string.
It works as expected, except for the fact it's matching any word that starts with 'admin', for example:
localhost/admin/news/list?page=1 => adm.php?route=news/list&page=1
localhost/adminbool/news/list?page=1 => adm.php?route=bool/news/list&page=1
Both of rewrite works, but that extra 'bool' messes everything in my routing process.
How can I make it respond only to a 'admin' exact match?
Any ideas?
Thanks!
EDIT
I guess im not clear enought.
Here is the full .htaccess and a brief comment about how its suppose to work
Options -Indexes
Options -MultiViews
<FilesMatch "\.(tpl|ini|log|txt)">
Order deny,allow
Deny from all
</FilesMatch>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(adm/|app/|lib/|sys/) - [F,L,NC]
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(?!admin/?$)(.+?)/?$ index.php?route=$1&%{QUERY_STRING} [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^admin(/.*)?$ adm.php?route=$1 [L,QSA]
</IfModule>
the index.php file refers to /app (front-end)
the adm.php refers to the /adm (back-end)
the urls of the front-end has no prefix, while the back-end urls has the /admin/ prefix
example:
localhost/admin - back-end
localhost/admin/ - back-end
localhost/admin/news/list?order=date - back-end
localhost/ - front-end
localhost/admine - front-end
localhost/administration-whatever.html - front-end
localhost/admine123-anything.html - front-end
Couple of issues:
Your regex is incorrect
You're not using helpful QSA flag.
Use this rule with correct regex and QSA:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^admin(/.*)?$ adm.php?route=$1 [L,QSA]
QSA (Query String Append) flag preserves existing query parameters while adding a new one.
Your rewrite rules:
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(adm/|app/|lib/|sys/) - [F,NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^admin(/.*)?$ adm.php?route=$1 [L,QSA]
RewriteCond %{REQUEST_URI} !/adm\.php [NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(?!admin/?$)(.+?)/?$ index.php?route=$1 [L,QSA,NC]
Try this:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^admin[$|/*](.+?)?$ adm.php?route=$1&%{QUERY_STRING} [L]

.htaccess - url rewrite that drops extension and passes argument

I'm looking for series of .htaccess statements that will convert the following urls
http://mysite.com/product to http://mysite.com/product.php
http://mysite.com/product/55 to http://mysite.com/product.php?id=55
http://mysite.com/category/38 to http://mysite.com/category.php?id=38
http://mysite.com/resources/car/19 to http://mysite.com/resources/car.php?id=19
http://mysite.com/resources/car/19?color=red&year=2013 to http://mysite.com/resources/car.php?id=19&color=red&year=2013
In other words, when rendering php files in my website, i want to drop the .php extension. If a url ends with a number, then i want to pass that as the id query string parameter. I also want to pass all the conventional query string parameters to my php my file like color and year.
I'm not sure how to construct such a .htaccess file.
ADDITIONAL NOTES
I'm currently using hte following, but it fails to take into consideration urls that trail with a number, and passing that along as id
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteCond %{QUERY_STRING} (.*)
RewriteRule . %{REQUEST_FILENAME}.php?%1 [L]
If I can do something like replace the trailing number in REQUEST_FILENAME in line two, thatwould be great.
First you need to make sure Multiviews is turned off. Then you'll need 3 sets of rewrite rules:
Options -Multiviews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ /$1.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([0-9]+)$ /$1.php?id=$2 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^resources/([^/]+)/([0-9]+)$ /resources/$1.php?id=$2 [L,QSA]
You can be a little more specific if the URLs are actually just "product", "category" and "car", then you can just have:
Options -Multiviews
RewriteEngine On
RewriteRule ^product$ /product.php [L]
RewriteRule ^(product|category)/([0-9]+)$ /$1.php?id=$2 [L,QSA]
RewriteRule ^resources/car/([0-9]+)$ /resources/car.php?id=$1 [L,QSA]
John (the op) says:
This was the final .htaccess file i ended up with
RewriteEngine On
Options -Multiviews
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} (.*)
RewriteRule ^(.*)\/([0-9]+)$ $1.php?id=$2&%1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} (.*)
RewriteRule ^(.*)$ $1.php?%1 [L]